Avatar of acbv
acbv
Flag 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!!!
Web Development

Avatar of undefined
Last Comment
Tom Beck

8/22/2022 - Mon
Tom Beck

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

acbv

ASKER
Is not working
Tom Beck

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
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
Tom Beck

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question