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
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 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.
How to display the category structure like WordPress using php?
Array
(
[0] => stdClass Object
(
[cat_id] => 64
[name] => Bathing Soap
[slug] => bathing-soap
[cat_taxonomy_id] => 65
[taxonomy] => product_cat
[parent] => 63
)
[1] => stdClass Object
(
[cat_id] => 65
[name] => Chemical
[slug] => chemical
[cat_taxonomy_id] => 66
[taxonomy] => product_cat
[parent] => 64
)
[2] => stdClass Object
(
[cat_id] => 63
[name] => Soap
[slug] => soap
[cat_taxonomy_id] => 64
[taxonomy] => product_cat
[parent] => 0
)
)
In your example is arrays as you store this records in database.
For output tree structure you should convert it to tree structure.
For example:
Change array and use cat_id as key in main array.
Add to each item field childs = array(); And store in this array ids for childs category.
Find root categories (where parent == 0) and save ids in childs field for item with key "0".
Step 1 you can make when you load data from fro database.
Steps 2 and 3 can make in one iteration (foreach)
After this your example will similar
array(
[0] => stdClass Object
(
[cat_id] => 0,
[childs] => array( [0]=>63 )
...
)
[63]=> stdClass Object
(
[cat_id] => 63,
[childs] => array( [0]=>64 )
...
)
Then you can output tree. Just begin with key = 0 and output all childs. For each child in first out title and then all childs. You should use recursion function.
I have tried this below link. you can also try this below Link
http://stevenbuick.com/category-hierarchy-with-codeigniter-and-jstree/
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.