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:
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:After that, you can “flatten” the array using
array_map()
so that it only contains the cat_ID if you wish: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:
in your case the object is
or by this formula as an array
in your case to get the value of cat_ID:
So your code will be something like: