0

I have the following object returned from a function

stdClass::__set_state(array(
   0 => 
  stdClass::__set_state(array(
     'term_id' => '175',
     'name' => 'a term',
     'slug' => 'a-term',
     'term_group' => '0',
     'term_taxonomy_id' => '177',
     'taxonomy' => 'category',
     'description' => '',
     'parent' => '174',
     'count' => '1',
  )),
   1 => 
  stdClass::__set_state(array(
     'term_id' => '182',
     'name' => 'aciform',
     'slug' => 'aciform',
     'term_group' => '0',
     'term_taxonomy_id' => '184',
     'taxonomy' => 'category',
     'description' => '',
     'parent' => '0',
     'count' => '1',
  )),
))

If I type cast this to an array, I get the following

array (
  0 => 
  stdClass::__set_state(array(
     'term_id' => '175',
     'name' => 'a term',
     'slug' => 'a-term',
     'term_group' => '0',
     'term_taxonomy_id' => '177',
     'taxonomy' => 'category',
     'description' => '',
     'parent' => '174',
     'count' => '1',
  )),
  1 => 
  stdClass::__set_state(array(
     'term_id' => '182',
     'name' => 'aciform',
     'slug' => 'aciform',
     'term_group' => '0',
     'term_taxonomy_id' => '184',
     'taxonomy' => 'category',
     'description' => '',
     'parent' => '0',
     'count' => '1',
  )),
)

The problem is, I need the inner objects to be type cast to arrays as well. I can do this by using a foreach loop and then type casting every value to an array and build a new array out of that, something like

foreach ($a as $b) {
    $c[] = (array) $b;
}
?><pre><?php var_dump($c); ?></pre><?php    

which does give me what I want

array(75) {
  [0]=>
  array(9) {
    ["term_id"]=>
    string(3) "175"
    ["name"]=>
    string(6) "a term"
    ["slug"]=>
    string(6) "a-term"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "177"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(3) "174"
    ["count"]=>
    string(1) "1"
  }
  [1]=>
  array(9) {
    ["term_id"]=>
    string(3) "182"
    ["name"]=>
    string(7) "aciform"
    ["slug"]=>
    string(7) "aciform"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "184"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
  }
}

My PHP knowledge are still quite limited, what I need to know is, is there another shorter better way to achieve this, or is this the only way.

EXTRA BACKGROUND

  • I cannot unfortunately change the output from the function. This is a function in Wordpress which only returns an object of objects and does not have an option to return an array of an array of values

  • I need to search for a specific term_id and returns its position. Again, I can use a foreach loop do do this, but PHP 5.5 + has a function array_column where you can use array_search to search for a specific value (in this case term_id) in a multidimensional array and return the key of that specific array.

1 Answer 1

4

I use json_encode and json_decode.

$json = json_encode($object);
$array = json_decode($json, true);

Or in one quick go:

$array = json_decode(json_encode($object), true);

It's pretty quick and works for all dimensions.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, did not think about that. Will need to look into those functions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.