Display the value from a returned object - php

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

Related

How to filter a category in wordpress/php?

I have a php code as shown below:
$category = get_the_category(); // Line #A
echo '<pre>'; print_r($category); echo '</pre>';// Line #B Added for debugging purpose
The code at Line#A retrieves post categories.
The code at the 2nd line which I have added for the debugging purpose returns the following array;
Array
(
[0] => WP_Term Object
(
[term_id] => 13085
[name] => Cannabis
[slug] => democracy_project_cannabis
[term_group] => 0
[term_taxonomy_id] => 13085
[taxonomy] => category
[description] => Hello World
[parent] => 13083
[count] => 8
[filter] => raw
[cat_ID] => 13085
[category_count] => 8
[category_description] => Good Morning
[cat_name] => Cannabis
[category_nicename] => democracy_project_cannabis
[category_parent] => 13083
)
[1] => WP_Term Object
(
[term_id] => 13093
[name] => Today
[slug] => today
[term_group] => 0
[term_taxonomy_id] => 13093
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[filter] => raw
[cat_ID] => 13093
[category_count] => 3
[category_description] =>
[cat_name] => Today
[category_nicename] => today
[category_parent] => 0
)
)
Problem Statement:
I am wondering what php code I need to add after Line#A so that it takes only category [name] => Today.
The code at Line#A returns the list of categories for a specific post. I just want to take only one category.
I think I need to use array_filter() method but I am not sure how I can use it.
I think you are better off getting the posts you want based a specific category to begin with rather than filtering everything.
$term = get_term_by('name', 'Today', 'category');
if ($term) {
$category = get_the_category($term->term_id);
} else {
echo "Category not found";
}
You can loop through the returned $category array:
foreach($category as $cat) {
if ($cat->name == 'Today') {
//do your stuff
}
}

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

Extract [cat_ID] from array PHP

I have the following array (just a part of the complete array) and I want to extract the value of [cat_ID].
Array ([1] => stdClass Object ( [term_id] => 21 [name] => z_aflyst [slug] => z_aflyst
[term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => category
[description] => [parent] => 0 [count] => 1 [object_id] => 7 [cat_ID] => 21
[cat_name] => z_aflyst ))
So I need to extract the 21 in this case. However, I only want to extract the cat_ID if the cat_name equals z_aflyst.
Give that you have an array of objects:
Array (
[1] => stdClass Object
(
[term_id] => 21
[name] => z_aflyst
[slug] => z_aflyst
[term_group] => 0
[term_taxonomy_id] => 23
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[object_id] => 7
[cat_ID] => 21
[cat_name] => z_aflyst )
)
foreach ($items as $item)
{
if($item->cat_name == 'z_aflyst')
{
//do stuff
}
}
You have an array of standard objects, which properties can be accessed using the arrow (object) operator ->.
You can use array_filter() to filter out unwanted elements of the array:
// Assuming your array is called $items
$filtered = array_filter($items, function($v) {
return $v->cat_name === 'z_aflyst';
});
After that, you can "flatten" the array using array_map() so that it only contains the cat_ID if you wish:
$filtered = array_map(function($v) {
return $v->cat_ID;
}, $filtered);
This will leave you with a one-dimension, indexed array of cat_IDs.
Your array is in this example a little more than a simple array, is a standard object. And you can take advantage of this. You can the properties of a standard object by this formula:
$object->property;
in your case the object is
$object = $array[1];
or by this formula as an array
$array[key];
in your case to get the value of cat_ID:
$object->cat_ID;
So your code will be something like:
if ($object->cat_name == 'z_aflyst') {
// do stuff with your cat_ID value
echo $object->cat_ID;
}
// will echo 21 only of the cat_name has value 'z_aflyst'

Simple PHP out of Wordpress Array / Object

Here is my code
$getcatid = get_the_category($id);
$postisin = $getcatid;
echo print_r($postisin);
This returns:
Array (
[0] => stdClass Object (
[term_id] => 8
[name] => High Net Worth Individuals
[slug] => high-net-worth-individuals
[term_group] => 0
[term_taxonomy_id] => 8
[taxonomy] => category
[description] =>
[parent] => 7
[count] => 1
[object_id] => 1266
[cat_ID] => 8
[category_count] => 1
[category_description] =>
[cat_name] => High Net Worth Individuals
[category_nicename] => high-net-worth-individuals
[category_parent] => 7
)
) 1
All that I want to do is grab the [term_id] and assign it as a variable to be used later.
In this case, '8' I'm not the best with PHP, but it's been a while since I've worked with Arrays / Objects and I've been unable to find a solution. I have also lost my Lynda login information > < Help please?
You can get it like this:
$postisin[0]->term_id
use loop
foreach ( $postisin as $rows ) {
echo $rows->term_id;
}
or if you want to echo only one index you can use
echo $postision[0]->term_id;
Try this:
$termId = $postisin[0]->term_id;

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.

Categories