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;
Related
I have a PHP array, assigned to $terms, from Wordpress that looks like this when printed:
Array ( [0] => WP_Term Object (
[term_id] => 19
[name] => Tshirts
[slug] => tshirts
[term_group] => 0
[term_taxonomy_id] => 19
[taxonomy] => clothes
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[1] => WP_Term Object (
[term_id] => 30
[name] => Pants
[slug] => pants
[term_group] => 0
[term_taxonomy_id] => 30
[taxonomy] => clothes
[description] =>
[parent] => 0
[count] => 12
[filter] => raw
)
)
I am trying to use in_array() to check if a value exists within the array, but I am having trouble since the array contains more parameters than just the categories. Let's say I want to see if pants exists in this array, here's what I've tried:
if (in_array('pants', $terms)) {
echo "Pants in array";
}
How can I modify this if statement so that it checks if pants exists as a slug in this array?
You can use array_search and array_column in combination. You'll want to turn the WP_Term object to an array too. Luckily it has a to_array() method for that so we can use array_map to convert them en masse in our function before searching the items:
var_dump(termsContainsSlug('pants', $terms)); // bool(true)
function termsContainsSlug($slug, $terms) {
$terms = array_map(function($term) { return $term->to_array(); }, $terms);
return array_search($slug, array_column($terms, 'slug')) > 0;
}
First, you should transform each object to an array and then test if there are any 'pants' value in.
foreach($terms as $term) {
if (in_array('pants', (array)$term)) {
echo "Pants in array";
}
}
This should transform WP_Term Object -> array
(array)$term
I have json string and I want to fetch product data from this string How can I achieve this. Please some one help me.
Below is my string,
{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}
There is a mix of JSON and PHP-serialized data.
<?php
$string = '{"num_rows":2,"row":{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},"rows":[{"setting":"a:6:{s:4:\"name\";s:9:\"Featutred\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"148\";}s:5:\"limit\";s:1:\"5\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"},{"setting":"a:6:{s:4:\"name\";s:17:\"Featured Products\";s:7:\"product\";a:2:{i:0;s:3:\"145\";i:1;s:3:\"146\";}s:5:\"limit\";s:1:\"4\";s:5:\"width\";s:3:\"200\";s:6:\"height\";s:3:\"200\";s:6:\"status\";s:1:\"1\";}"}]}';
$dataObject = json_decode($string);
foreach($dataObject->rows as $row){
$productData = unserialize($row->setting);
print_r($productData);
}
Will result in
Array
(
[name] => Featutred
[product] => Array
(
[0] => 145
[1] => 148
)
[limit] => 5
[width] => 200
[height] => 200
[status] => 1
)
Array
(
[name] => Featured Products
[product] => Array
(
[0] => 145
[1] => 146
)
[limit] => 4
[width] => 200
[height] => 200
[status] => 1
)
NOTE: There is no error checking in code above, since it is written for your particular example. If you are not sure that your input data is correct (that is usual), you need to check if JSON is ok, object is object and also has needed properties, etc.
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 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
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.