1

So I'm busy at creating a system. That system generates a bit of JSON

For example:

{
    "Builds": [
        {
            "title": "test",
            "item1": "3",
            "item2": "3",
            "item3": "0",
            "item4": "0",
            "item5": "0",
            "item6": "0"
        },
        {
            "title": "test2",
            "item1": "3",
            "item2": "3",
            "item3": "2",
            "item4": "0",
            "item5": "0",
            "item6": "0"
        }
    ]
}

Now im trying to get every "build" with a foreach in PHP this only loops the first time and then does nothing.

$i = 0;

foreach($builds as $build){
    //echo $i;
    echo $build[$i]['title'];
    echo '<img src="' . $build[$i]['item1'] . '" alt=""></img>';
    $i++;
}
3
  • 3
    Why are you using a counter? Get rid of it. echo $build['title']; etc Commented Nov 12, 2015 at 19:14
  • @JohnConde The json file makes me using the counter. Unless im going with another JSON structure. But thats still not an answer to my question, about how to solve it. Cuase i think it is strange it only loops one time.. Commented Nov 12, 2015 at 19:18
  • should be foreach($builds['Builds'] as $build){ ` Commented Nov 12, 2015 at 19:19

3 Answers 3

1

Because of the for loop, you don't need a counter.

$json = '{"Builds":[{"title":"test","item1": "3","item2": "3","item3": "0","item4": "0","item5": "0","item6": "0"},{"title":"test2","item1": "3","item2": "3","item3": "2","item4": "0","item5": "0","item6": "0"}]}';
$data = json_decode($json, true);
$builds = $data['Builds'];
foreach($builds as $build){
    echo $build['title'];
    echo '<img src="' . $build['item1'] . '" alt=""></img>';
}

The only reason for a counter would be, if you want to iterate over the item elements in your builds.

for ($i = 1; isset($build['item' . $i]); $i++) {
    echo '<img src="' . $build['item' . $i] . '" alt=""></img>';
}
Sign up to request clarification or add additional context in comments.

7 Comments

If i do it without the coutner it still doesnt work. I am getting an undefinded index error.
Probably want foreach($builds['Builds']
If you get an undefined index error, your $build var doesn't contain the content of the json you posted - if you tried my code, you would see, that it actually worked
Does it? $build['item1'] is an object: $build->item1, $build->title and $data->Builds
@HasseBjörk json_decode as second param, which indicates that the return value is an assoc array
|
1

You're working with an array of objects. This is how you should be doing it:

<?php

$json = ""; //your JSON string
$obj = json_decode($json);
foreach ($obj->Builds as $build) {
    echo $build->title;
    echo "<img src='$build->item1' alt=''>";
}

?>

1 Comment

Im using json_decode($build,true) which only returns arrays:)
0

This is a working version

$builds = '{"Builds":[{"title":"test","item1": "3","item2": "3","item3": "0","item4": "0","item5": "0","item6": "0"},{"title":"test2","item1": "3","item2": "3","item3": "2","item4": "0","item5": "0","item6": "0"}]}';

foreach(json_decode( $builds )->Builds as $build ){
    echo $build->title;
    echo '<img src="' . $build->item1 . '" alt=""></img>';
}

First you need to use the json decoded object Builds to iterate through. This will give you objects to display, like $build->title

2 Comments

They arent objects though cause i use json_decode($builds,true) only arrays are given back as return value
I did not see that in your example... Well, this does work!

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.