1
{  
"something":"not important;",
"something2":"less important",
"something3":"inexistent",
"something4":"nothing",
"something5":{  
        "1232":{  // this value is dynamic
        "NeedToEchoThis":1230,
        "NeedToEchoThis2":"12343",
        "NeedToEchoThis3":22222,
  }
},
  "something6":"else",
"something7":"0"
}

When using json_decode, I get that array. But... how to echo the values within "something5" since it has a dynamic value right next to it? I tried using

$var1 = $obj->something5[0]->NeedToEchoThis;

But... still doesn't work. Any ideas?

Random Guy
  • 19
  • 3

3 Answers3

0

You can e.g. iterate over the array something5 with a foreach loop.

<?php
$data = json_decode(data(), true);

foreach( $data['something5'] as $k=>$v) {
    if ( isset($v['NeedToEchoThis']) ) {
        echo $k, ' -> NeedToEchoThis => ', $v['NeedToEchoThis'], "\r\n";
    }
}



function data() {
    return <<< eoj
{
    "something": "not important;",
    "something2": "less important",
    "something3": "inexistent",
    "something4": "nothing",
    "something5": {
        "1232": {
            "NeedToEchoThis": "1230",
            "NeedToEchoThis2": "12343",
            "NeedToEchoThis3": "22222"
        }
    },
    "something6": "else",
    "something7": "0"
}
eoj;
}

prints

1232 -> NeedToEchoThis => 1230
VolkerK
  • 95,432
  • 20
  • 163
  • 226
0

It really depends on how you use json_decode.

When you json_decode($input) you get an object (not an array) that contains other objects.

$obj = json_decode($input);
echo $obj->something5->{'1232'}->NeedToEchoThis;

Or:

$obj = json_decode($input, true);
echo $obj['something5'][1232]['NeedToEchoThis'];

Here is a complete runnable example:

$input = <<<EEE
{  
"something":"not important;",
"something2":"less important",
"something3":"inexistent",
"something4":"nothing",
"something5":{  
    "1232":{  
        "NeedToEchoThis":1230,
        "NeedToEchoThis2":"12343",
        "NeedToEchoThis3":22222
    }
},
  "something6":"else",
  "something7":"0"
}
EEE;
echo $input . "\n";
$obj = json_decode($input);
var_dump($obj);

echo $obj->something5->{'1232'}->NeedToEchoThis;
echo "\n";

// If you don't know the key names:

$vars = get_object_vars ( $obj );
foreach($vars as $name => $value ) {
    if ( is_object($value) ) {
        echo "$name => "; print_r($value);
    } elseif( is_string($value) ){
        echo "$name => $value\n";        
    }
}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
0

As you mention dynamic value it's not really clear what you mean. The notation in your json example is not clear also.

In case the "1232" value changes, you could get the dynamic value of the element as follows:

if( !empty($obj->something5) ) { 
    $arr = (array) $obj->something5[0];
    if( is_array($arr) && !empty($arr) ) {
        $key = array_keys( $arr )[0];
        $var1 = $obj->something5[0]->{$key}->NeedToEchoThis;
    } else {
        $var1 = "nothing to show";
    }

} else { 
    $var1 = "nothing to show";
}
Evhz
  • 8,852
  • 9
  • 51
  • 69