Home > front end >  I can't read JSON in php
I can't read JSON in php

Time:03-28

I have this small snippet of code in PHP to read a JSON sent by Ajax, and when I read it, the return is always empty.

<?php
    
$json='{"count":3,"value":[{"Code":"1","Name":"Carlos"},{"Code":"2","Name":"Charles" },{"Code":"3","Name":"Joseph"}]}';

$data = json_decode($json,true);

foreach($data as $row){
    $code=$row['Code'];    
    $name=$row['Name'];
}

Thank You.

CodePudding user response:

You were close. $data['value'] has the list of items.

foreach ($data['value'] as $row) {
    print_r($row);
    $code = $row['Code'];
    $name = $row['Name'];
}

CodePudding user response:

try foreach($data["value"] as $row) instead of foreach($data as $row)

  • Related