Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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?

share|improve this question
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? –  Barmar Jun 27 '14 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. –  MrLore Jun 27 '14 at 23:27

1 Answer 1

up vote 1 down vote accepted

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

}
share|improve this answer
    
Thanks a lot, seems to work perfect –  KlintMX Jun 27 '14 at 23:43
    
Tried to give You more than one vote, but I couldn't –  KlintMX Jul 2 '14 at 20:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.