douwen7905 2017-09-11 14:07
浏览 37
已采纳

如何在PHP中访问嵌套数组的stdClass对象[重复]

This question already has an answer here:

I have php array like

Array 
( 
[0] => stdClass Object 
    ( [type] => MILESTONE
      [creator] => xyz 
      [tpid] => abc 
      [docname] => STS 
      [items] => stdClass Object 
        ( 
            [MILESTONE_CODE] => ARPOD 
            [MILESTONE_TYPE] => ACT 
            [MILESTONE_DESCRIPTION] => Arrival at Port of Discharge 
            [TRIGGER_EVENT] => Y [ENVIRONMENT] => T 
        ) 
    ) 
[1] => stdClass Object 
    ( 
        [type] => MILESTONE 
        [creator] => xyz 
        [tpid] => abc 
        [docname] => STS 
        [items] => stdClass Object 
        ( 
            [MILESTONE_CODE] => BKD 
            [MILESTONE_TYPE] => EST 
            [MILESTONE_DESCRIPTION] => Booking created 
            [TRIGGER_EVENT] => N 
            [ENVIRONMENT] => P 
        ) 
    ) 
)

There are 2 php files written

File 1: In this file I am decoding my json data and got above array

$data = json_decode(file_get_contents("php://input"));

 // print_r($data);exit;

foreach ($data as $value){

$TP->createTPConfig($data);  // Here I am calling method for each index of array
} 

File 2: In this file I have written method to insert data into oracle database.

function createTPConfig($dataToBeInsert){

// print_r($dataToBeInsert);exit; // till here I am getting above array

// written insert query here 

}

Now in File 2, I want to access array values of

[type], [creator], [tpid], [docname]

and again

I want to foreach(repeat) loop for [items]

and want to access keys and values of

[MILESTONE_CODE], [MILESTONE_TYPE], [MILESTONE_DESCRIPTION] and [TRIGGER_EVENT]

and insert both keys and values into database

Can anyone help ... your help would be appreciated!

</div>
  • 写回答

1条回答 默认 最新

  • dpeqsfx5186 2017-09-11 14:44
    关注

    Just change

    $data = json_decode(file_get_contents("php://input"));
    

    to

    $data = json_decode(file_get_contents("php://input"), true);
    

    This will give you a full-fledged array. Obligatory man link: json decode

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

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题