Display values from function that returns multidimensional array

I have a function that returns a multidimensional array.

[tt]get_field(‘authors’)[/tt]

For each author it stores 11 fields of data.

I want to display specific values for each author (e.g. array locations [5] and [7]). But when I do a test:

[php]
<?php
$authors_deets = get_field(‘authors’);
$authors_number = count($authors_deets);

		echo "<p>variable value: " . $authors_deets . "</p>";
		echo "<p>count array: " . $authors_number . "</p>";
 		echo  "<p>author 0, field 5: " . $authors_deets[0][5] . "</p>";
		echo "<p>";
		foreach($authors_deets as $x => $x_value) {
			echo "Key=" . $x;
			echo "Key value>?=" . $x[5];
			echo "<br>";
		}	
		echo "</p>";
		
		echo var_dump($authors_deets);
	?>	

[/php]

The results are:

[tt]
variable value: Array

count array: 2

author 0, field 5:

Key=0Key value>?=
Key=1Key value>?=

[/tt]

I expect the “author 0, field 5:” to be followed by the 5th array value of the first array item: “Tom Smith http://testwebsite.com/user/tomsmith” but it’s just blank.

Then I set up the foreach thinking I could pull the secondary array values out that way, but they also are just blank, even though there is data there.

Here is the var_dump from this function result with two top-level arrays:

[tt]

array(2) { \[0\]=> array(11) { ["ID"]=> string(1) "5" ["user_firstname"]=> string(6) "Tom" ["user_lastname"]=> string(6) "Smith" ["nickname"]=> string(3) "TAS" ["user_nicename"]=> string(3) "tas" ["display_name"]=> string(13) "Tom Smith" ["user_email"]=> string(10) "[email protected]" ["user_url"]=> string(64) "http://testwebsite.com/users/tomsmith.html" ["user_registered"]=> string(19) "2016-02-25 21:38:03" ["user_description"]=> string(0) "" ["user_avatar"]=> string(121) "<img src="http://testwebsite.com/img/users/tomsmith96.png" width="96" height="96" alt="Tom Smith" class="avatar photo" />" } [1]=> array(11) { ["ID"]=> string(1) "6" ["user_firstname"]=> string(7) "Michele" ["user_lastname"]=> string(7) "Lee" ["nickname"]=> string(3) "MKL" ["user_nicename"]=> string(3) "mkl" ["display_name"]=> string(15) "Michele Lee" ["user_email"]=> string(10) "[email protected]" ["user_url"]=> string(66) "http://testwebsite.com/users/michellelee.html" ["user_registered"]=> string(19) "2016-02-25 21:39:02" ["user_description"]=> string(0) "" ["user_avatar"]=> string(127) "<img src="http://testwebsite.com/img/users/michellelee96.png" width="96" height="96" alt="Michelle Lee" class="avatar photo" />" } } [/tt]

I’d greatly appreciate any help with learning to get specific values out of a multidimensional array that is the result of a function.

Thanks

You are trying to get the display name from each author?

You could try: $authors_deets[0][‘display_name’]. How the information is available, is partly due to how it is returned. You can return values based on an index or a associative array key.

Try this,

[php]for( $i = 0; $i < count($authors_deets); $i++){
echo “

Author: {$authors_deets[$i][‘display_name’]}

”;
}[/php]

astonecipher, thanks! That’s definitely giving me traction, as it’s returning values.

I have to step out for a short time, but when I get back I’m going to try to build off of your example to also display the values of the second-level array for each author.

This definitely worked. I was able to get everything I needed to display, including author images.

Thanks for your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service