1
{
"line1": 12,
"line2": 4,
"line3": 66,
"line4": false,
"line5": true,
"line6": [
    {
        "line7": "false",
        "line8": "true"
    },
    {
        "line9": "false",
        "line10": "false"
    }
],
"line11": 0,
"line12": 0,

Using json_decode, I can extract elements from array with the following method:

$showline1 = $obj->line1;

$showline2 = $obj->line2;

But how to extract line7, line8, line9 and line10? I tried using:

$showline7 = $obj->line6->line7; But it didn't worked.

Community
  • 1
  • 1
Ronnix
  • 11
  • 5

4 Answers4

1

If you look at closely the "line6" has more than one elements inside, So basically you will get array when you decode by json_decode. In Short to get line7 you need to use $data->line6[0]. To get all inside elements you need to use loop obviously.

mit
  • 59
  • 5
0

line6 is an array so you need target index try

$showline7 = $obj->line6[0]->line7;

hope it helps :)

FastTurtle
  • 1,747
  • 11
  • 15
0

Did you try:

json_decode($your_json_string,true);

This will return an array instead of object, and you can get line7 value like this:

$obj["line6"][0]["line7"]

Or as your object, you can get line7 value like this:

$obj->line6[0]->line7
Newbie
  • 287
  • 1
  • 8
0

I hve to add another answer here, because the code is long.

This is my test, and it works fine. Please take a look.

<?php
$data=json_decode('{
"line1": 12,
"line2": 4,
"line3": 66,
"line4": false,
"line5": true,
"line6": [
    {
        "line7": "false",
        "line8": "true"
    },
    {
        "line9": "false",
        "line10": "false"
    }
],
"line11": 0,
"line12": 0}');
echo $data->line6[1]->line9;
exit;
?>
Newbie
  • 287
  • 1
  • 8