0

I have the following JSON code.

{
  "Artikelnr":"1000",
  "Vooraad_NL":2.0,
  "Voorraad_DE":1.0,
  "Voorraad_BE":0.0
}

When I loop through the json with a foreach it loops 4 times. How can I make it loop only through the amount of articles. Like 1 article loops only once. When there are 3 articles in the json loop three times.

I use the following php code:

$data = json_decode(file_get_contents('php://input'), true);

if ((isset($data) && $data != "") && (json_last_error() === JSON_ERROR_NONE)) {

foreach ($data as $key => $val) {
    $articlenumber = $data['Artikelnr'];
    $voorraad_nl = $data['Vooraad_NL'];
    $voorraad_de = $data['Vooraad_DE'];
    $voorraad_be = $data['Vooraad_BE'];
}

}
9
  • Do you build the JSON? If so putting that in array would allow 1 loop. Otherwise can use if($key != 'Artikelnr') { continue; } if you only want loop to run on "Artikelnr":"1000", data point. Commented Oct 5, 2023 at 20:11
  • The JSON is send by the Microsoft API. When a product is sold on our website, the Microsoft Business Central API sends an update of the stock. Commented Oct 5, 2023 at 20:16
  • And it it sent like that or like [{ "Artikelnr":"1000", "Vooraad_NL":2.0, "Voorraad_DE":1.0, "Voorraad_BE":0.0 }]? Like that all points would be content at one level. As is they are all at the same level. I don't see how they could send back 2 results at the same time with that structure Commented Oct 5, 2023 at 20:27
  • 1
    When i run your code with simple echo inside foreach it only loop, and echo each item just once Commented Oct 5, 2023 at 20:54
  • 1
    So you basically need to write a condition: php is variable an array or object If your script requires an objectArray, but you have an object, you need to push the object into a parent array. Your question is not crystal clear about the variable payloads that your application is receiving. Please create a complete minimal reproducible example. Commented Oct 5, 2023 at 23:46

2 Answers 2

4

You have the following json

{
  "Artikelnr":"1000",
  "Vooraad_NL":2.0,
  "Voorraad_DE":1.0,
  "Voorraad_BE":0.0
}

So, when you looping it after json_decode, you are getting something like this:

Array
(
    [Artikelnr] => 1000
    [Vooraad_NL] => 2
    [Voorraad_DE] => 1
    [Voorraad_BE] => 0
)

So, when you are looping the $data:

$data = json_decode(file_get_contents('php://input'), true);

foreach ($data as $key => $val) {
    $articlenumber = $data['Artikelnr'];
    $voorraad_nl = $data['Vooraad_NL'];
    $voorraad_de = $data['Vooraad_DE'];
    $voorraad_be = $data['Vooraad_BE'];
}

The loop is iterating over the $data array which contains 4 items and you are not using the $key/$val inside your loop but the $data itself so your seeing the same items 4 times.

if you have only one json then you don't need to loop, you can simply do the same thing without the loop, for example:

echo $data['Artikelnr'];
echo $data['Vooraad_NL'];
echo $data['Voorraad_DE'];
echo $data['Voorraad_BE'];

If you are not sure whether the result is an array of objects or a single object, you may try something like this:

// Decode without passing the true in the json_encode
$data = json_decode(file_get_contents('php://input'));

if (is_object($data)) {
    $data->Artikelnr;
    $data->Vooraad_NL;
    $data->Vooraad_DE;
    $data->Vooraad_BE;
} else {
    foreach ($data as $item) {
        $articlenumber = $item->Artikelnr;
        $voorraad_nl   = $item->Vooraad_NL;
        $voorraad_de   = $item->Vooraad_DE;
        $voorraad_be   = $item->Vooraad_BE;
    }
}

if you want to use each item as an array then you may convert the object (stdClass) using (array), for example:

foreach ($data as $item) {
    $item = (array) $item;
    // Now you can use the item as an array
    $articlenumber = $item['Artikelnr'];
    // ...
}

Same could be possible for the single object, for example:

if (is_object($data)) {
    $data = (array) $data;
    $articlenumber = $data['Artikelnr'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

I recommend conditionally converting any encountered objects to a objectArrays and unconditionally writing a loop to iterate the objectArray.

All applications should endeavor to stabilize their input/output data types for more streamlined scripting (a shame that the endpoint that you are hitting doesn't make this consideration).

Code: (Demo)

$data = json_decode($response);
if (is_object($data)) {
    $data = [$data];
}
foreach ($data as $obj) {
    var_dump($obj);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.