0

I'm trying to take a JSON string i'm receiving from an API and process the contents with PHP. I just can't figure out how to get the info out. I convert the JSON to an object with json_decode and try to get the products' array with dot and bracket syntax (see my commented lines in code snippet). Could somebody tell me what I am missing here? I haven't used PHP in many many years.

<?php
$sampleJson = '{"products": [{"id": 6806481862794,"title": "Berry Soap"},{"id": 6806481600650,"title": "Lavender Soap"},{"id": 6806481666186,"title": "Vanilla Soap"},{"id": 6806481993866,"title": "Aloe Soap"}]}';

echo $sampleJson;

$sampleObject = json_decode($sampleJson);

echo "<br/>------<br/>";
var_dump($sampleObject);
echo "<br/>------<br/>";
//var_dump($sampleObject.products);
//var_dump($sampleObject[products]);
var_dump($sampleObject['products']);
turkeyhundt
  • 214
  • 1
  • 15

0 Answers0