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
Related
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
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.
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
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove index key from Array for accessing to object?
I need to be able to access the term_id value but I don't know the number or index assossiated with the array. How can I access it?
I would to access it like this $value->term_id, right now I need to access it by putting the number after the value ($value->[26]->term_id).
Array
(
[26] => stdClass Object
(
[term_id] => 26
[name] => Night Life.
[slug] => shopping-and-night-life
[term_group] => 0
[term_taxonomy_id] => 28
[taxonomy] => map_categories
[description] => Most of the late night clubs, bars and pubs in Victoria are situated downtown. Here are a few to check out:
[parent] => 0
[count] => 6
[object_id] => 925
)
)
You could use array_values() to "reset" the array indices:
$new = array_values($old);
Would result with
Array
(
[0] => stdClass Object
(
[term_id] => 26
[name] => Night Life.
[slug] => shopping-and-night-life
[term_group] => 0
[term_taxonomy_id] => 28
[taxonomy] => map_categories
[description] => Most of the late night clubs, bars and pubs in Victoria are situated downtown. Here are a few to check out:
[parent] => 0
[count] => 6
[object_id] => 925
)
)
Regardless of what the previous array index was.
Right now it is an array so you would access it like this: $value[26]->term_id if you don't want to have to put the key you would just need to set another variable equal to the object inside the array:
$value2 = $value[26];
echo $value2->term_id;
If you don't know if the value 26 then use a foreach.
foreach($value as $key => $val) {
$term_id = $val->term_id;
}
Of if you know there is only one element in the array you could do this:
$value2 = end($value);
$term_id = $value2->term_id;
You have to either search for it OR write an algorithm whereby you do not lose it in the first place.
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.