0

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!

philip
  • 484
  • 9
  • 26
  • @deceze, below code worked for me and this kind of scenario I could not find on stackoverflow File 1: $data = json_decode(file_get_contents("php://input"),true); if($data != null){ $TP->createTPConfig($data); } File 2: function createTPConfig($dataToBeInsert){ for ($x=0;$x $value){ print_r($key . $value); } } – Choudhary Pukhraj Sep 13 '17 at 07:29

1 Answers1

1

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

rndus2r
  • 496
  • 4
  • 17