skip to Main Content

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'];
}

}

2

Answers


  1. The reason your loop is running four times is that you are looping through the keys in the JSON object, and your JSON object has four key-value pairs. If you want to loop through the articles based on the "Artikelnr" key and its values, then you could probably do this:

    $data = json_decode(file_get_contents('php://input'), true);
    
    if (isset($data) && !empty($data) && json_last_error() === JSON_ERROR_NONE) {
        // Check if the data is an array
        if (is_array($data)) {
            foreach ($data as $article) {
                $articlenumber = $article['Artikelnr'];
                $voorraad_nl = $article['Vooraad_NL'];
                $voorraad_de = $article['Vooraad_DE'];
                $voorraad_be = $article['Vooraad_BE'];
                
                // Process each article here
                echo "Article Number: $articlenumber<br>";
                echo "Voorraad NL: $voorraad_nl<br>";
                echo "Voorraad DE: $voorraad_de<br>";
                echo "Voorraad BE: $voorraad_be<br><br>";
            }
        }
    }
    
    Login or Signup to reply.
  2. 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'];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search