ARRAYs

how can return the array index in the below example? in last statement shows the value of BMW. but I need to know how get the BMW as output. is it possible in php?

[php]$car_price[“BMW”]=‘25bn’;
$car_price[“Ferrari”]=‘23bn’;
$car_price[“volvo”]=‘19bn’;

echo $car_price[“BMW”];[/php]

Are you trying to get the item in the array where the value (e.g. ‘25bn’) matches what you’re looking for?

You could use a foreach:

[php]$found = false;
$car_make = ‘’;

foreach($car_price as $key => $value) {
if($key == ‘WHAT_YOU_ARE_LOOKING_FOR’) {
$found = true;
$car_make = $key;
}
}[/php]

yah thank your reply I got a new idea from your answer.

above statement [php]echo car_price [“BMW”];[/php] out put is 25bn, I need to get the BMW as a answer how can I give a code to the OUT PUT OF BMW what is the input to get this? can we get it?

Replace $key with $value in the IF:

[php]$car_price[“BMW”]=‘25bn’;
$car_price[“Ferrari”]=‘23bn’;
$car_price[“volvo”]=‘19bn’;

$found = false;
$car_make = ‘’;

foreach($car_price as $key => $value) {
if($value == ‘25bn’) {
$found = true;
$car_make = $key;
}
}

echo $found ? $car_make : ‘Not found!’;[/php]

Try it: http://codepad.org/Zm4vLh3b

Thank you very much.

Sponsor our Newsletter | Privacy Policy | Terms of Service