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 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题
  • ¥15 一道计算机组成原理问题