'I can't read JSON in php

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.



Solution 1:[1]

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

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cornelb
Solution 2 Elio Bteich