0

$response is the data am getting when accessing an external API. i wanted to access the value of the key fileList. I tried like dd($result['serverResponse']['extensionServiceState']['payload']['fileList']) , but it returns error undefined index extensionServiceState . How can i achieve the required result?

$response = curl_exec($curl); $result = json_decode($response, true); //var_dump($result); dd($result['serverResponse']['extensionServiceState']['payload']['fileList']); 

before json decode

""" {"resourceId":"abcd","sid":"123","serverResponse":{"extensionServiceState":[{"payload":{"fileList":[{"filename":"1.m3u8","sliceStartTime":16439789992},{"filename":"2.mp4","sliceStartTime":16439789992}],"onhold":false,"state":"exit"},"serviceName":"web_recorder_service"},{"payload":{"uploadingStatus":"uploaded"},"serviceName":"upload_service"}]}} 

"""

after json decode

 array:3 [ "resourceId" => "abcd" "sid" => "123" "serverResponse" => array:1 [ "extensionServiceState" => array:2 [ 0 => array:2 [ "payload" => array:1 [ "uploadingStatus" => "uploaded" ] "serviceName" => "upload_service" ] 1 => array:2 [ "payload" => array:3 [ "fileList" => array:2 [ 0 => array:2 [ "filename" => "1.m3u8" "sliceStartTime" => 1643986576940 ] 1 => array:2 [ "filename" => "2.mp4" "sliceStartTime" => 1643986576940 ] ] "onhold" => false "state" => "exit" ] "serviceName" => "web_recorder_service" ] ] ] ] 

var_dump($$result) after json decode shows the below data

 array(3) { ["resourceId"]=> string(4) "abcd" ["sid"]=> string(3) "123" ["serverResponse"]=> array(1) { ["extensionServiceState"]=> array(2) { [0]=> array(2) { ["payload"]=> array(1) { ["uploadingStatus"]=> string(8) "uploaded" } ["serviceName"]=> string(14) "upload_service" } [1]=> array(2) { ["payload"]=> array(3) { ["fileList"]=> array(2) { [0]=> array(2) { ["filename"]=> string(45) "1.m3u8" ["sliceStartTime"]=> int(16439789992) } [1]=> array(2) { ["filename"]=> string(46) "2.mp4" ["sliceStartTime"]=> int(16439789992) } } ["onhold"]=> bool(false) ["state"]=> string(4) "exit" } ["serviceName"]=> string(20) "web_recorder_service" } } } } 
2
  • Can you var_dump($result); Commented Feb 4, 2022 at 14:35
  • @executable please see the updated question body.I've added the result of var_dump Commented Feb 4, 2022 at 14:48

1 Answer 1

1

Either you pasted the wrong content or json_decode is lying to you. You asked for an associative array from json_decode (by passing the second argument as true). The result of that should actually look like this:

 Array ( [resourceId] => abcd [sid] => 123 [serverResponse] => Array ( [extensionServiceState] => Array ( [0] => Array ( [payload] => Array ( [fileList] => Array ( [0] => Array ( [filename] => 1.m3u8 [sliceStartTime] => 16439789992 ) [1] => Array ( [filename] => 2.mp4 [sliceStartTime] => 16439789992 ) ) [onhold] => [state] => exit ) [serviceName] => web_recorder_service ) [1] => Array ( [payload] => Array ( [uploadingStatus] => uploaded ) [serviceName] => upload_service ) ) ) ) 

So therefore, to get what you want you would need to do this:

dd($result['serverResponse']['extensionServiceState'][0]['payload']['fileList']); 

But your data contains multiple items inside extensionServiceState so you may want to loop over those.

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

3 Comments

Please see the updated question body.i've added the result of var_dump after json decoding
what u said is right. the content i pasted first was wrong. now, i've pasted the correct content. please see the updated question body
i tried if(!empty($result['serverResponse']['extensionServiceState'][0]['payload']['fileList'])). this check always returns false although the array fileList has values.Can u tell me why it behaves so?