KlintWeb
KlintWeb

Reputation: 31

PHP foreach - parsed json from an array inside an array

Here is a part of the json file:

{
  "status": {
     "http_code": 200
  },
  "contents": [
    {
      "FabrikatNavn": "Jaguar",
      "ModelNavn": "420G",
      "PrisDetailDkk": 119900,
      "StatusTyper": [
        {
          "StatusId": -5,
          "StatusNavn": "Benzin"
        },
        {
          "StatusId": -15,
          "StatusNavn": "Momsfri"
        },
        {
          "StatusId": -11,
          "StatusNavn": "100-120.000"
        }
      ],


      "ImageIds": [
        {
          "Id": 79359
        },
        {
          "Id": 79360
        },
        {
          "Id": 79361
        },
        {
          "Id": 79370
        }
      ]
    },
    {
      "FabrikatNavn": "Opel",
      "ModelNavn": "Corsa",
      "PrisDetailDkk": 135900,
      "StatusTyper": [
        {
          "StatusId": -4,
          "StatusNavn": "Diesel"
        },
        {
          "StatusId": -15,
          "StatusNavn": "Momsfri"
        },
        {
          "StatusId": -12,
          "StatusNavn": "120-140.000"
        }
      ],


      "ImageIds": [
        {
          "Id": 225794
        },
        {
          "Id": 225795
        },
        {
          "Id": 225796
        },
        {
          "Id": 225797
        }
      ]
    },
    {
      "FabrikatNavn": "Hyundai",
      "ModelNavn": "H1",
      "PrisDetailDkk": 14999,
      "StatusTyper": [
        {
          "StatusId": 13,
          "StatusNavn": "Afhentning"
        },
        {
          "StatusId": -4,
          "StatusNavn": "Diesel"
        },
        {
          "StatusId": -8,
          "StatusNavn": "0-60.000"
        }
      ],


      "ImageIds": [
        {
          "Id": 415605
        },
        {
          "Id": 415606
        },
        {
          "Id": 415607
        },
        {
          "Id": 415979
        }
      ]
    }
  ]
}

And here's the PHP

<?php

$url = 'http://banen.klintmx.dk/json/ba-simple-proxy.php?url=api.autoit.dk/car/GetCarsExtended/59efc61e-ceb2-463b-af39-80348d771999';
$json= file_get_contents($url);

$data = json_decode($json);
$rows = $data->{'contents'};
foreach ($rows as $row) {
    echo '<div>';
    $FabrikatNavn = $row->FabrikatNavn;
    $ModelNavn = $row->ModelNavn;
    $PrisDetailDkk = $row->PrisDetailDkk;

    // tried this, but it don't work -->
    foreach($row->StatusTyper as $StatusTyper) {
        $StausId = $StatusTyper->StatusId;
        if ($StausId == '-15') { $Moms = 'mm'; }
        else { $Moms = 'um'; }
    }

    echo '<div class=" ' . $Moms . ' "> ';
    echo $FabrikatNavn . $ModelNavn . ' Pris ' . $PrisDetailDkk;
    echo '</div>';
    echo '</div>';
}

As you can see, the values ​​in the "StatusTyper" different, so here I have tried with if else, but I can not get it to work - It returns'um' everytime What's wrong?

Upvotes: 0

Views: 305

Answers (1)

Rahil Wazir
Rahil Wazir

Reputation: 10132

This is because the loop always picks the value of last condition met. You need to somehow tell PHP to break the loop if it founds the match $StausId == "-15" otherwise it will continue to match $StausId != "-15" which always assigns the value "um" to $Moms.

foreach($row->StatusTyper as $StatusTyper) {
    $StausId = $StatusTyper->StatusId;

    if ($StausId == "-15") {
        // Found the match
        $Moms = 'mm';
        // End the execution of current loop
        break;
    } else {
        // $StausId value is not equal to -15
        // So it was picking this
        $Moms = 'um';
    }

}

Upvotes: 1

Related Questions