Link to home
Create AccountLog in
Avatar of acbv
acbvFlag for Canada

asked on

PHP Multidimensional array show only one value

Hello I have this array:
 [41] => Array
        (
            [type] => locations
            [name] => LOCATION
            [keyword] => Array
                (
                    [5] => City
                    [4] => Region
                    [3] => Regional District
                    [2] => Province
                    [1] => LOCATION1
                )

            [value] => Array
                (
                    [5] => Grande Prairie
                    [4] => Grande Prairie
                    [3] => Grande Prairie
                    [2] => Alberta
                    [1] => Canada
                )
How can I modify this code in order to show only the city?:

foreach ($property[41]['value'] as $ind=>$lval)
            {
                  $location_string .= $lval .', ';
            }
Thanks!!!
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

How about this?:
foreach ($property[41] as $ind=>$lval)
            {
		if ($ind === "keyword" && is_array($lval)) {
			$location_string .= $property[41]['value'][array_search("City", array_keys($lval))] . ', ';
		}
            }

Open in new window

Avatar of acbv

ASKER

Is not working
Sorry to hear that.

Based on what you have posted as the contents of index 41, the code I provided should loop through the contents, look for "keyword" in the $ind variable key then check to see that its value is an array. When the conditions are met, it pulls the value positioned at the same index as "City" from the "keyword" sub array from the "value" sub array. Simple logic, I don't see anything wrong with my code so I cannot explain why it's not working for you.

Perhaps you can provide more information or maybe a PHP expert will assist further.
Avatar of acbv

ASKER

I found it:
if (isset( $property[41]))
                  $location_string .= $property[41]['value'][5];

Thanks anyway for your help, very much appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account