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!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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


Open in new window

Julian HansenCommented:
Hint: in future if you do print_r do it like this
echo '<pre>' . print_r($terms, true) . '</pre>';

Open in new window

Note the second parameter true which tells print_r to return the data as a string rather than output it to the screen.

Also take a look at var_dump - if you need to see the data in a form that is easy to migrate into code again.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dfehlingAuthor Commented:
Thank you for such fast help!
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

dfehlingAuthor Commented:
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

Julian HansenCommented:
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
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.
dfehlingAuthor Commented:
No, this is perfect! All I needed was to list all the names. Thanks again!
Ray PaseurCommented:
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.
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.