We help IT Professionals succeed at work.

PHP Multidimensional array show only one value

acbv asked
Last Modified: 2014-05-09
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 .', ';
Watch Question

Top Expert 2015

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


Is not working
Top Expert 2015

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.


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

Thanks anyway for your help, very much appreciated!
Top Expert 2015
This one is on us!
(Get your first solution completely free - no credit card required)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.