Link to home
Start Free TrialLog in
Avatar of dfehling
dfehling

asked on

Need help in accessing specific elements of an array

If I use:

<?php  print_r($terms); ?>

Open in new window


I get:

Array ( [0] => stdClass Object ( [term_id] => 331 [name] => John [slug] => john [term_group] => 0 [term_taxonomy_id] => 331 [taxonomy] => pairing [description] => [parent] => 209 [count] => 2 [object_id] => 6981 [filter] => raw ) [1] => stdClass Object ( [term_id] => 209 [name] => Mary [slug] => mary [term_group] => 0 [term_taxonomy_id] => 209 [taxonomy] => pairing [description] => [parent] => 0 [count] => 2 [object_id] => 6981 [filter] => raw ) )

However, if I use:

<?php  echo $terms['name'];  ?>

Open in new window


I don't get anything.

How do I get just the items in the 'name' field of this array displayed, in this case "John, Mary".

Thanks in advance!
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Lets look at your output formatted nicely
Array ( 
	[0] => stdClass Object ( 
		[term_id] => 331 
		[name] => John 
		[slug] => john 
		[term_group] => 0 
		[term_taxonomy_id] => 331 
		[taxonomy] => pairing
		[description] =>
		[parent] => 209
		[count] => 2
		[object_id] => 6981
		[filter] => raw )
	[1] => stdClass Object (
		[term_id] => 209
		[name] => Mary
		[slug] => mary
		[term_group] => 0
		[term_taxonomy_id] => 209
		[taxonomy] => pairing
		[description] =>
		[parent] => 0
		[count] => 2
		[object_id] => 6981
		[filter] => raw ) 
)

Open in new window

Now you can see you actually have an array of objects so what you actually want is

$terms[0]->name

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dfehling
dfehling

ASKER

Thank you for such fast help!
Hi - I thought this was working but it is only returning the first "name" in my array - ie. John, not John, Mary.

What am I missing? I used:

<?php  echo $terms[0]->name; ?>

Open in new window

That is to access the name in the first term - there are two items in the array so the next name would be

<?php echo $terms[1]->name;?>

Open in new window

Or to dump all names of terms in the array
<?php
foreach($terms as $term) {
    echo $term->name . '<br/>';
}

Open in new window

To answer your code I need to know what you were expecting? You have an array of records with more than one term - each with its own name. Do you want to dump all of them or access a specific one? You need to explain your intent.
No, this is perfect! All I needed was to list all the names. Thanks again!
For future reference, PHP arrays and objects work the same in many ways, but there is a difference in syntax.  $terms['name'] make reference to an associative array with an element that has the key, "name."  In the case of the object, the correct syntax would be $terms->name.  

If you're new to PHP and want to learn the language, this article can guide you to dependable resources.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
You are welcome.