1

how can I get the same results/values in one loop rather than two?

$json = file_get_contents($url) 
$data = json_decode($json, true);


$desc = $data["descriptions"];
$assets = $data["assets"];

foreach ($assets as $assItem) {
    echo $assItem["assetid"];
}


foreach($desc as $descItem) {
    echo descItem["name"];
}

I've tried something like

$json = file_get_contents($url);
$data = json_decode($json, true);

foreach ($data as $item) {
    echo $item["assets"]["assetid"];
    echo $item["descriptions"]["name"];
}

pastebin to the json: https://pastebin.com/raw/uA9mvE2e

3
  • I have deleted my answer as the question is not clear. Paste var_dump($data) and expected output. So that we can help you better. Commented Apr 22, 2017 at 13:36
  • post the json file format Commented Apr 22, 2017 at 13:37
  • added pastebin to the json in main post Commented Apr 22, 2017 at 13:44

1 Answer 1

1

You could do something like:

$json = file_get_contents($url);
$data = json_decode($json, true);

foreach ($data['assets'] as $k => $item) {
    echo $item["assetid"];
    echo $data["descriptions"][$k]["name"];
}

This assumes that $data['assets'] and $data['descriptions'] share the same indices.

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

Comments

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.