1

I have a nested array that I converted to JOSN using json_encode(), and when I tried to convert it back to an array form using json_decode() the returned result was empty.

$arr = json_decode($json,true);

below is the result of print_r for the json variable:

[{
    "result":"SUCCESS",
    "msg":{
        "type":"localScan",
        "res":{
            "status":"0",
            "scan":[
                {
                    "name":"AVG",
                    "result":"0",
                    "type":"Clean"
                },{
                    "name":"ESET NOD32",
                    "result":"2",
                    "type":"Not Working"
                },{
                    "name":"AVAST",
                    "result":0,
                    "type":"Clear"
                },{
                    "name":"Kaspersky",
                    "result":"2",
                    "type":"Not Working"
                },{
                    "name":"Bit-defender",
                    "result":"2",
                    "type":"Not Working"
                }
            ]
        }
    }
},{
    "result":"SUCCESS",
    "msg":{
        "type":"localScan",
        "res":{
            "status":"1",
            "scan":[{
                    "name":"AVG",
                    "result":"1",
                    "type":"Autorun"
                },
                {
                    "name":"ESET NOD32",
                    "result":"2",
                    "type":"Not Working"
                },
                {
                    "name":"AVAST",
                    "result":2,
                    "type":"Not Working"
                },
                {
                    "name":"Kaspersky",
                    "result":"2",
                    "type":"Not Working"
                },
                {
                    "name":"Bit-defender",
                    "result":"2",
                    "type":"Not Working"
                }
            ]
        }
    }
}]

This is a partial array that is used to create the JSON, the json_decode function return value is FALSE.

Array ( 
    [0] => Array ( 
        [result] => SUCCESS 
        [msg] => Array ( 
            [type] => localScan 
            [res] => Array ( 
                [status] => 0 
                [scan] => Array ( 
                    [0] => Array ( 
                        [name] => AVG 
                        [result] => 0 
                        [type] => Clean 
                    ) 
                    [1] => Array ( 
                        [name] => ESET NOD32 
                        [result] => 2 
                        [type] => Not Working 
                    ) 
                    [2] => Array ( 
                        [name] => AVAST 
                        [result] => 0 
                        [type] => Clear 
                    ) 
                    [3] => Array 
                    ( 
                        [name] => Kaspersky 
                        [result] => 2 
                        [type] => Not Working 
                    ) 
                    [4] => Array ( 
                        [name] => Bit-defender 
                        [result] => 2 
                        [type] => Not Working 
                    )
                )
            ) 
        ) 
    )
)
5
  • Can you provide a PHP array that can reproduce this problem? Commented Apr 27, 2015 at 7:15
  • Seems valid json. Please provide what you are getting and show your efforts. Commented Apr 27, 2015 at 7:16
  • try var_dump($arr); to see what you are getting actually. Commented Apr 27, 2015 at 7:49
  • have you die()'d script after json_encode ? Commented Apr 27, 2015 at 7:50
  • I have exit(), I'm returning the json from a web service which I built using the tutorial in this link: link Commented Apr 27, 2015 at 7:54

2 Answers 2

1

Following the code that you can try as i have taken the data from your asked question :-

$jsonString='<String of the Json>'; $data = json_decode($jsonString); echo '<pre>'; print_r($data);

Hop this will help you

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

3 Comments

Thank you, this code works... but why? I was using something similar!
send me the code that you were using so that i can give you appropriate answer on why your code did not run
I found my problem, I was unintentionally returning other results besides json string and the json_decode() was unable to decode that. It was solved when I removed them. thank you again.
0

I think you didn't echo the json string -

<?php
  $arr = array(); // your array
  echo json_encode( $arr );
?>

2 Comments

I used print_r(), I also used sizeof() to find the result length and it was 0.
The code itself is very huge and the array is built by getting the results from different parts of it. if there is a problem it might be in the array structure or the encode/decode of the json string.

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.