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.
Related
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
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
https://gist.github.com/jcranny/9465715
I have this array (example)...
Array
(
[0] => Array
(
[series] => stdClass Object
(
[term_id] => 5
[name] => Moto2 2013
[slug] => moto2-2013
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 77
[team] => Technomah carXpert
[constructor] => Suter
[machine] => Honda CBR600RR
)
[1] => Array
(
[series] => stdClass Object
(
[term_id] => 6
[name] => Moto2 2014
[slug] => moto2-2014
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 15
[team] => La Fonte Tascaracing
[constructor] => Suter
[machine] => Honda CBR600RR
)
[2] => Array
(
[series] => stdClass Object
(
[term_id] => 7
[name] => Moto2 2015
[slug] => moto2-2015
[term_group] => 0
[term_taxonomy_id] => 3
)
[race_number] => 15
[team] => Mapfre Aspar Team Moto2
[constructor] => Suter
[machine] => Honda CBR600RR
)
)
Now I would like to be able to get information from each block.
For example I would like to echo this data:
[race_number]
[team]
[constructor]
[machine]
But I want only to echo this data that is relevant to a specific [series]
I have the [series] term_id so this is my key to get the relevant data, but i'm struggling to get my node function work.
This is the function to do this:
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);
foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]] = $field_val;
}
return $new_fields;
}
Then this is how I am get the series specific data based on the series term id.
$rider_series_data = node_modify_riders_array($rider_id);
$series_id = $series[0]->term_id;
$team = $rider_series_data[$series_id]["team"];
$contstructor = $rider_series_data[$series_id]["constructor"];
$machine = $rider_series_data[$series_id]["machine"];
$race_number = $rider_series_data[$series_id]["race_number"];
But some thing is wrong and I can work it out. Can anyone see where I'm going wrong or help me fix it.
Massive thanks in advance, would really appreciate some help.
What the problem is:
My function node_modify_riders_array is returning null, which is causing my $team, $contstructor, $machine, and $race_number too not output anything.
If I echo $series_id on my example, I get 6
Which should pass though my node_modify_riders_array function and display the relevant array values. But it's not outputting anything and I got no errors.
This is my full code so you can see what I am trying to do...
https://gist.github.com/jcranny/9465715
You are using an object as your array key and not the term_id of your series object.
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);
foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]->term_id] = $field_val;
//^^^^^^^^^ <--- add this
}
return $new_fields;
}
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'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.