0

I have a JSON arraylist , Need to read data from it using php. I have tried it and failed simply. I will give all in the following

Input data: JSON

{    

    "orderHistory":[
        {
        "product_name": "mike",
        "product_price": 15000,
        "product_id": 5
        },
        {
        "product_name": "tv",
        "product_price": 25000,
        "product_id": 3
        }
    ]
}

PHPcode

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

while ($row = $data->fetch(PDO::FETCH_ASSOC)){

        extract($row);
echo '{';
        echo '"message": "array added."';

    echo '}';

} 

Error :

<b>Fatal error</b>: Uncaught Error: Call to undefined method stdClass::fetch() in

Joas
  • 1,796
  • 1
  • 12
  • 25
learner
  • 1
  • 4

1 Answers1

0

your error is about the undefined method there is no function with name fetch in your class $data recheck your $data class


please check below codes. i use your json and access to all data you can act with your data as array like this

$str = '{"orderHistory":[{"product_name": "mike","product_price": 15000,"product_id": 5},{"product_name": "tv","product_price": 25000,"product_id": 3}]}';
$a = json_decode($str);
echo (json_encode($a->orderHistory));
echo ($a->orderHistory[0]->product_name);
echo ($a->orderHistory[1]->product_name);
Mhmd
  • 406
  • 5
  • 12