0

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?

2
  • 1
    At the end of the inner foreach loop, $Moms contains the value from the last element of StatusTyper. Which one do you want to use instead? Commented Jun 27, 2014 at 23:26
  • What is your expected output? I don't think many people here will have a grasp on Danish VAT rates on cars. Commented Jun 27, 2014 at 23:27

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

Tried to give You more than one vote, but I couldn't

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.