How to loop through this json decoded data in PHP?

后端 未结 5 1031
一向
一向 2020-12-10 21:40

I\'ve have this list of products in JSON that needs to be decoded:

\"[{\"productId\":\"epIJp9\",\"name\":\"Product A\",\"amount\":\"5\",\"identifier\":\"242\         


        
5条回答
  •  抹茶落季
    2020-12-10 22:03

    Try like following codes:

    $json_string = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
    
    $array = json_decode($json_string);
    
    foreach ($array as $value)
    {
       echo $value->productId; // epIJp9
       echo $value->name; // Product A
    }
    

    Get Count

    echo count($array); // 2
    

提交回复
热议问题