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'];
}
if($key != 'Artikelnr') { continue; }if you only want loop to run on"Artikelnr":"1000",data point.[{ "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