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!

7
  • 1
    Welcome to SO! Please take the tour and read up on How to ask a good question. 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. Commented Aug 22, 2018 at 8:51
  • Please explain fully what you require and what the problem is as this is a bit unclear Commented Aug 22, 2018 at 8:54
  • 1
    Have you got as far as json_decode() on that string? Commented Aug 22, 2018 at 8:55
  • eval.in/1049773 Commented Aug 22, 2018 at 8:56
  • 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 Commented Aug 22, 2018 at 9:06

3 Answers 3

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.

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

2 Comments

Thank you so much!
Welcome..... :)
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'];
}

2 Comments

A foreach would be Soooooo much simpler
Thanks for Answering, but it does not work with the rest of my code
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>";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.