doukang5966907 2017-09-18 15:36
浏览 11
已采纳

Php从Array更改为Objects

I am using woocommerce 2.6 now on woocommerce 3.0 output of $item->get_items has changed from Array to Objects.

I want to convert object std class to array by key name to fetch data. Below is output of custom woocommerce 3.x vardump.

Please help me on this.

[1]=>
object(stdClass)#18913 (3) {
  ["id"]=>
  int(3944)
  ["key"]=>
  string(5) "Vuxen"
  ["value"]=>
  string(15) "2 (4 800,00 Kr)"
}
[2]=>
object(stdClass)#18912 (3) {
  ["id"]=>
  int(3945)
  ["key"]=>
  string(18) "Totalt biljettpris"
  ["value"]=>
  string(11) "4 800,00 Kr"
}
[3]=>
object(stdClass)#18911 (3) {
  ["id"]=>
  int(3946)
  ["key"]=>
  string(30) "jrp_name_adult_ordinary_7day_1"
  ["value"]=>
  string(26) "undefined/kjhjksh/jdhfjshk"
}
[4]=>
object(stdClass)#18910 (3) {
  ["id"]=>
  int(3947)
  ["key"]=>
  string(13) "Nationality_1"
  ["value"]=>
  string(25) "Nej, ej på japanskt pass"
}
[5]=>
object(stdClass)#18909 (3) {
  ["id"]=>
  int(3948)
  ["key"]=>
  string(30) "jrp_name_adult_ordinary_7day_2"
  ["value"]=>
  string(33) "undefined/jhdsfjjhdkjs/jkdshfjshj"
}
[6]=>
object(stdClass)#18908 (3) {
  ["id"]=>
  int(3949)
  ["key"]=>
  string(13) "Nationality_2"
  ["value"]=>
  string(21) "Ja, på japanskt pass"
}
[7]=>
object(stdClass)#18907 (3) {
  ["id"]=>
  int(3950)
  ["key"]=>
  string(11) "Avresedatum"
  ["value"]=>
  string(10) "30/09/2017"
}
[8]=>
object(stdClass)#18906 (3) {
  ["id"]=>
  int(3951)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(65) "Jag har förstått villkoren för användning av Japan Rail Pass."
}
[9]=>
object(stdClass)#18905 (3) {
  ["id"]=>
  int(3952)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(88) "Jag förstår att det är mitt ansvar att se till att jag har korrekt visum i mitt pass."
}
[10]=>
object(stdClass)#18904 (3) {
  ["id"]=>
  int(3953)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(88) "Jag har förstått villkoren för användning av Japan Rail Pass som japansk medborgare."
}
[11]=>
object(stdClass)#18903 (3) {
  ["id"]=>
  int(3954)
  ["key"]=>
  string(12) "Sätt kryss:"
  ["value"]=>
  string(84) "Jag förstår att det är mitt ansvar att se till att jag har korrekt dokumentation."
}
  • 写回答

5条回答 默认 最新

  • doudong1117 2017-09-21 07:03
    关注

    You can use the following code to get all objects to an array.If the key in the objects is found multiple times it will create unique array by key name and fetch all value from all objects to an unique key.

    $order = wc_get_order($order_id);
    foreach ($order->get_items() as $item_id => $item_obj) {
        $item_data = $item_obj->get_data();
        foreach ($item_data as $key => $object) {
        }
        for($m=0;$m<count($object);$m++) {
            $new_array[$object[$m]->key][$m] = $object[$m]->value;
        }
    }
    echo "<pre>";var_dump($new_array);echo "</pre>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行