0

im working on a project but I ran into a problem :( I looked on the internet for like 2-3 hours and cannot find the way how to do it...

I retrieve this JSON from my database:

{
"articles": {
    "1": {"discription":"Transport", "tax":"21", "price":"234"
    },
    "2": {"discription":"Opslag", "tax":"19", "price":"19"}
    }
}

What I want to do next is retrieve the description,tax & Price from each object, how to do this with a for each loop on the most efficient way?

Thanks!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
tbijl
  • 27
  • 8
  • 1
    Welcome to SO! Please take the [tour](https://stackoverflow.com/tour) and read up on [How to ask a good question](https://stackoverflow.com/help/how-to-ask). In its current state, we can't really help you with your problem as it is unclear what the problem is, what you've tried and where you got stuck. – Loek Aug 22 '18 at 08:51
  • Please explain fully what you require and what the problem is as this is a bit unclear – Marius Brits Aug 22 '18 at 08:54
  • 1
    Have you got as far as [`json_decode()`](http://php.net/manual/en/function.json-decode.php) on that string? – RiggsFolly Aug 22 '18 at 08:55
  • https://eval.in/1049773 – NanThiyagan Aug 22 '18 at 08:56
  • @MariusBrits, I think it's actually pretty clear what he want's from this. He just wants to access the attributes of each object... no? – Rafael Aug 22 '18 at 09:05
  • 1
    @Rafael I think what is clear is that OP wants someone else to do it for them. We all know that _Whats the most efficient way to do..._ is code for ___do it for me___ But people keep encouraging questions like this by answering them – RiggsFolly Aug 22 '18 at 09:06
  • @RiggsFolly, that might be the case also, but not in any sense this makes the task unclear, does it? – Rafael Aug 22 '18 at 09:11

3 Answers3

5

You can use following code snippet to get value of mentioned keys-

<?php

$str         = '{
        "articles": {
            "1": {"discription":"Transport", "tax":"21", "price":"234"
            },
            "2": {"discription":"Opslag", "tax":"19", "price":"19"}
            }
        }';
//convert json to array
$jsonToArray = json_decode($str, true);
foreach ($jsonToArray['articles'] as $value) {
    echo $value['discription'] . ' ' . $value['tax'] . ' ' . $value['price'] . PHP_EOL;
}

if you want to access those keys with object then you can proceed with following code snippet-

<?php

$str          = '{
        "articles": {
            "1": {"discription":"Transport", "tax":"21", "price":"234"
            },
            "2": {"discription":"Opslag", "tax":"19", "price":"19"}
            }
        }';
//convert json to object
$jsonToObject = json_decode($str);
foreach ($jsonToObject->articles as $value) {
    echo $value->discription . ' ' . $value->tax . ' ' . $value->price . PHP_EOL;
}

You are free to make changes in the echo statement that suits your requirement. Checkout my all the technical post.

Exception
  • 789
  • 4
  • 18
1
$array = json_decode($json_string);
for($i = 0; $i<sizeof($array); $i++) {
  echo $array[$i]['description'];
  echo $array[$i]['tax'];
  echo $array[$i]['price'];
}
Dieter Kräutl
  • 657
  • 4
  • 8
1

You are looking for the json_decode function before you do that, here's an example:

$jsonDecoded = json_decode($jsonInput);

foreach ($jsonDecoded->articles as $item) {
   echo $item->discription . "<br>";
   echo $item->tax . "<br>";
   echo $item->price . "<br>";
}
Rafael
  • 1,495
  • 1
  • 14
  • 25