duanli6834 2015-04-19 16:57
浏览 52
已采纳

键入将多维对象数组转换为多维数组

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条回答 默认 最新

  • dongliu6848 2015-04-19 17:01
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备