3

I´m trying to put JSON-Information into a PHP variable and I´m getting Errors all the time.

This is my Code to make the JSON readable:

header('Content-Type: application/json');
$jsondecode1 = json_decode($json1);
print_r ($jsondecode1);

This is a snippet of the $jsondecode1:

Array
(
    [0] => stdClass Object
        (
            [flightId] => ******
            [******] => stdClass Object
                (
                    [******] => ******
                    [******] => ******
                    [******] => ******
                    [******] => ******
                    [******] => ******                
                )

Notice: When I echo the jsondecode1 it outputs this:

Array to string conversion in C:\xampp......\simpletest3.php on line 48

So I used print_r().

What I tried to put (for example) [flightId] into a PHP Variable:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1->flightId;
print_r ($jsondecode2);

Output: Notice: Trying to get property 'flightId' of non-object in C:.........\simpletest3.php on line 48

I tried some other codes too, but the Outputs were very similar and I don´t want to make my Question longer than it needs to be. So, how do I put (for example) the [flightId] into a PHP variable.

Edit:

Solution:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
print_r ($jsondecode2);

Or:

foreach ($jsondecode1 as $data) {
    print_r($data->flightId);
}
0

3 Answers 3

2

You have to do it like below (what you shown)

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
echo $jsondecode2;

But you have multi-dimensional-array,so do like below:-

$jsondecode1 = json_decode($json1);

foreach ($jsondecode1 as $jsondecode) {
    echo $jsondecode->flightId; // here you can use echo $flightId = $jsondecode->flightId; 
}
Sign up to request clarification or add additional context in comments.

2 Comments

@duffbeerisgood also when you used json_decode() it converts the json string into object array
@duffbeerisgood for the current scenario, yes, as you want to iterate over json data and try to get the values form it.
2

Your flightId is inside one array whoch index is 0. So you have to first access that array then you can get flightId

try below code:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
print_r ($jsondecode2);

Comments

2

Your json contains an array of objects, not 1 object.

$jsondecode1 = json_decode($json1);

// loop through all objects in the array
foreach ($jsondecode1 as $data) {
    print_r($data->flightId);
}

Or if you only want to have the first object of the array:

$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
print_r ($jsondecode2);

But then the question is why is the json an array and not a single object?


Also you can not simply echo or print out non-scalar types (like arrays or objects), for that you will have to use print_r.

3 Comments

Great Solution. Thank you !
I have no Idea why the json is an array, I thought you guys could tell me that.
@duffbeerisgood That is your input, so I can not tell you why it is like that :) $json1 is a string containing something like [{"flightId": "******", ...}]. That is an array of objects. If it were to have {"flightId": "******", ...}, without the [ and ] around it, it's a single json object.

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.