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

$terms[0]->name

Open in new window

1
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.
1

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!
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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

0
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
<?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.
0
dfehlingAuthor Commented:
No, this is perfect! All I needed was to list all the names. Thanks again!
0
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
Julian HansenCommented:
You are welcome.
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.