Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
JSON
$json = '{{"var1":"9","var2":"16","var3":"16"}, 
               {"var1":"8","var2":"15","var3":"15"}}';


What I have tried:

JSON
$json = '{{"var1":"9","var2":"16","var3":"16"}, 
                {"var1":"8","var2":"15","var3":"15"}}';
    $decoded = json_decode($json);
    foreach ($decoded as $k => $v) {
        echo "<pre>";
        print_r($k)."<br>";
        echo "";    }
Posted
Updated 1-Nov-23 9:05am
v2

1 solution

It's not much of a question you've asked, but I will say that that isn't valid JSON. If it's meant to be an array of values then it would need to be:
'[{"var1":"9","var2":"16","var3":"16"}, 
{"var1":"8","var2":"15","var3":"15"}]';

Note that I replaced the first and last '{' and '}' with '[' and ']' respectively, because that's what signifies an array in JSON.

Also your foreach loop is printing the $k variable which is likely to be just the index of the element in the array, not that value at that specific index.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900