dfehling
asked on
Need help in accessing specific elements of an array
If I use:
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:
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!
<?php print_r($terms); ?>
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']; ?>
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for such fast help!
ASKER
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:
What am I missing? I used:
<?php echo $terms[0]->name; ?>
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;?>
Or to dump all names of terms in the array<?php
foreach($terms as $term) {
echo $term->name . '<br/>';
}
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.
ASKER
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
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.
Open in new window
Now you can see you actually have an array of objects so what you actually want isOpen in new window