Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find array / dictionary value using key?

Tags:

arrays

php

key

I would like to find the value in an array using the key.

Like this:

$array=('us'=>'United', 'ca'=>'canada'); $key='ca'; 

How can I have the value 'canada'?

like image 847
JEagle Avatar asked Jun 04 '10 00:06

JEagle


People also ask

How do you find the value of the dictionary using the key?

You can use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.

How do you find the key and value of an array?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

Can a dictionary key be an array?

A dictionary is sometimes called an associative array because it associates a key with an item. The keys behave in a way similar to indices in an array, except that array indices are numeric and keys are arbitrary strings. Each key in a single Dictionary object must be unique.


1 Answers

It's as simple as this :

$array[$key]; 
like image 57
HoLyVieR Avatar answered Oct 07 '22 19:10

HoLyVieR