PHP accessing an array - php

I'm a bit confused.
I have an array:
<?php
$terms = get_the_terms($post->ID, 'pf');
print_r($terms);
?>
And it outputs:
Array ( [15] => stdClass Object (
[term_id] => 15 [name] => Text [slug]
=> text [term_group] => 0 [term_taxonomy_id] => 33 [taxonomy] =>
pf [description] => An PF article.
[parent] => 0 [count] => 3 [object_id]
=> 694 ) )
And I want just to output slug ("text" in this case) instead of the whole array.
So I'm doing:
<?php $terms = get_the_terms($post->ID, 'pf');
echo $terms["slug"]; ?>
And it outputs nothing.
This gives no results as well:
echo "{$terms['slug']}";
Any ideas?
UPDATED!!!
I can't use $term[15]->slug since my script will be based on [taxonomy] (pf in this case)! :) So it's impossible to do that without foreach loop?

terms array 15 index contain object access like this
echo $term[15]->slug

there is stdclass object at index 15 of the inside arry which can be converted/accessed as array by casting but try this insted
$term[15]->slug

Following up on Pekka's answer, if you reformat your print_r() output, you'd get:
Array (
[15] => stdClass Object (
[term_id] => 15
[name] => Text
[slug] => text
[term_group] => 0
[term_taxonomy_id] => 33
[taxonomy] => pf
[description] => An PF article.
[parent] => 0
[count] => 3
[object_id] => 694
)
)
When dumping out a variable with print_r(), it's good practice to surround the call with <pre> tags - print_r doesn't do any HTML-ification of the data, so the nice indentation it does with arrays gets lost when viewed in an HTML page. Using the <pre> tags preserves the formatting. using var_dump() will do the same, but also add type/size data to the dump output.

Related

No ajax response due to echo array

I have troubles with a website filter, where I would like to echo information of an array, but doing this the the ajax response will just stop.
This is the array, that is printed with print_r
Array (
[0] => WP_Term Object (
[term_id] => 181
[name] => Football
[slug] => football
[term_group] => 0
[term_taxonomy_id] => 181
[taxonomy] => activities
[description] =>
[parent] => 0
[count] => 3
[filter] => raw )
Here is the code to print it this way:
$activities = get_the_terms($lesson_id,'activities',true);
print_r ($activities);
Now, I only want to print the name and I tried to use implode:
echo implode(', ',$activities) ;
But, with this line the Ajax response is empty. Same thing happens, when I simply echo one value from Array 0 or when I loop with foreach:
echo $activities[0]['name'];
The intention is to just print out the string "Football".
Your print_r tell that the array contain a collection of WP_Term objects so you should use
echo $activities[0]->name;

Display a single element of an array

I think it is a very simple question but for a long time I am trying to figure this out:
I need to store the number 18 ([term_id]) from the following array in a variable, how can I do this?
Array (
[0] => WP_Term Object (
[term_id] => 18
[name] => Im Rebgarten
[slug] => im-rebgarten
[term_group] => 0
[term_taxonomy_id] => 18
[taxonomy] => give_forms_category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[object_id] => 900
)
)
Kind regards
You can simply access the variable from object by -> notation like
$array = array();//Your current array
$term_variable = $array[0]->term_id; //store the term_id into the variable
print_r($term_variable); //prints the variable

Merging Arrays on values (different keys) PHP

I would like to display a set of answers to a specific question - with different answers based on a country - no problem with the foreach side of this.
However, I only have access (in this array - $answers) to an ID value for the country name. This ID will match another Key in a different array ($countries) and I'd like to output the country name instead of the ID value.
Where the [ques_jurisduction] ID matches the term_id value below, I'd like to append and output the value in [name] - essentially allowing me to replace the ID in the output with the correct country name.
Print_r for each gives:
$answers
Array ( [ques_jurisdiction] => 5 [ques_answer] => )
$countries
Array ( [1] => WP_Term Object ( [term_id] => 119 [name] => Austria [slug] => austria [term_group] => 0 [term_taxonomy_id] => 119 [taxonomy] => wgd_jurisdiction [description] => [parent] => 0 [count] => 96 [filter] => raw ) [value] => WP_Term Object ( [term_id] => 119 [name] => Austria [slug] => austria [term_group] => 0 [term_taxonomy_id] => 119 [taxonomy] => wgd_jurisdiction [description] => [parent] => 0 [count] => 96 [filter] => raw ) [0] => 0 [key] => 0 )
Any guidance most appreciated. Think I might be getting a bit confused with Taxonomies and sub_fields!
Thanks
As per my understanding, you want to output name of country and you have its term_id as in ques_jurisdiction index of $answers array. I also noticed that your index 1 and value contains same values in $countries array. If I am right till now then you can achieve what you want like this:
foreach($countries['value'] as $obj){
if($obj->term_id === $answers['ques_jurisdiction']){
echo $obj->name; //It will output country name, you can as well take it in some variable to use it later on
}
}
I have used 'value' index, you can as well use index 1 as they both contains same data.
I hope it helps

PHP working with arrays

I have this line of code:
print_r(get_the_terms( $_product->id, 'product_cat'));
which returns:
Array ( [0] => stdClass Object ( [term_id] => 67 [name] => Paintings [slug] => paintings [term_group] => 0 [term_taxonomy_id] => 67 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 44 ) [1] => stdClass Object ( [term_id] => 13 [name] => Small [slug] => small [term_group] => 0 [term_taxonomy_id] => 13 [taxonomy] => product_cat [description] => [parent] => 0 [count] => 15 ) )
what I am trying to get is [name] => Paintings so I can create another array like so:
$array[get_the_terms( $_product->id, 'product_cat')->name] = $_product->get_title()
$_product->get_title() is "A Quiet Day"
expected output Array ( [Paintings] => A Quiet Day )
If I do this:
$array[] = $_product->get_title();
the output is
Array ( [0] => A Quiet Day )
I am just trying to replace the 0 with Paintings
$array['Paintings'] = $_product->get_title();
If you want the array to have specific keys, specify them...
The problem may be that it looks like the class you're storing that name in is not properly defined. Are you storing it in the session?
Your get_the_terms() function also appears to return more than one object, so you will not be able to chain ->name off it. You'll need to select the right one.
You need to make your code and question more readable.

Display the value from a returned object

I have a variable object which stores the value below.
Array
(
[0] => stdClass Object
(
[term_id] => 1
[name] => Uncategorized
[slug] => uncategorized
[term_group] => 0
[term_taxonomy_id] => 1
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 4
[object_id] => 39
[cat_ID] => 1
[category_count] => 4
[category_description] =>
[cat_name] => Uncategorized
[category_nicename] => uncategorized
[category_parent] => 0
)
)
I now want to display the slug from the list of values. How do i do so?
Assuming that object is stored in an array called $array:
echo $array[0]->slug
You can use print_r($yourObject) to print out the entire object for debugging or if you want to print just that value this would work: echo($yourObject[0]->slug); .
If there are multiple array indices then this:
foreach($yourObject as $object)
{
echo $object->slug;
}
Jonh gave you an exact answer...let me explain it a bit
$obj = your_array;//from where you are using var_dump() to see these values..
and then
echo $obj->slug
you can use the same technique for other terms in your dump, like
echo $obj->name
HTH

Categories