0

I can show the FactorDate item with the following code.

$response=json_decode($response->getBody());
foreach($response as $product)
{
    echo $product->FactorDate;
}

but I can not show the Price item. please guide me.

[ { "CustomerCode": 101, "FactorNumber": 53, "FactorDate": 14010201, "FactorDetails": [ { "ProductCode": 21901, "Count": 15, "Price": 96000000, "VisitorID": 0 } ] } ]

1 Answers1

0

The JSON that you had provided looks like price is a part of an array within FactorDetails. Meaning that if you want to get the price you will likely have to do something like:

$product->FactorDetails[0]->Price

Or put into your example, something like this will display both Date and Price:

$response=json_decode($response->getBody());
foreach($response as $product)
{
    echo $product->FactorDate;
    echo $product->FactorDetails[0]->Price;
}

That should work.

For future reference, you could use a tool like this JSON viewer to see a more readable version of your JSON which can be super helpful when dealing with formatting issues like this one:

https://codebeautify.org/jsonviewer

Ervin Šabić
  • 115
  • 3
  • 14
  • Thank you very much my friend. It was done correctly with your formula – saman shirmohammadi Apr 24 '22 at 19:35
  • @samanshirmohammadi see if there is a reason why FactorDetails is an array. If it's something you can help, I would try to turn FactorDetails into an object itself so you can just do something like `$product->FactorDetails->Price`. I feel like it's a bit hacky referring to the first object of the array by doing `[0]`. – Ervin Šabić Apr 24 '22 at 19:53
  • I'm doing my job exactly with your code and it is done correctly. Thank you – saman shirmohammadi Apr 27 '22 at 21:29