1

I have a proper .json physical file and i read it from PHP by parsing it.

Let's say the sales.json contains:

{
    "custid" : "7761", 
    "items" : [
        {
            "itemcode"  : "A11231G",
            "suppliers" : [
                {
                    "id" : "s10001",
                    "name" : "Benny & John",
                },
                            {
                    "id" : "s10004",
                    "name" : "Colorado Dimension",
                }
            ]
        }
    ]
}

Then i consume it from PHP:

$sales = json_decode( file_get_contents("store/sales.json"), true );

There is no problem and $sales is already become an Array which is ok.

Now for some reason, i want to feed that json_decode() function with an PHP Array (instead of the .json physical file).


I know it is the dumb way that i am actually doing like converting, Array -> json -> Array, which is finally Array to Array.


But for whatever reason i have,

  • Even if i have a PHP Array() with the correct structure, if i use json_encode($phpArray), then to feed as json_decode( json_encode($phpArray), true ), will it give the exact object like i get from json_decode("sales.json") file?
  • (or) how can i feed the json_decode function with a PHP Array() object i have?
7
  • 1
    Sounds like an XY problem. What exactly are you trying to achieve? Commented Mar 10, 2014 at 14:12
  • jsonlint.com says its not a valid json which is posted on the question. Commented Mar 10, 2014 at 14:14
  • you can store the array in a file with serialize() then unserialize and json encode tuxradar.com/practicalphp/5/11/0 Commented Mar 10, 2014 at 14:33
  • Why not change your array directly? Commented Mar 11, 2014 at 1:54
  • @xdazz (not really but you can imagine) the php file which contains $sales = json_decode( file_get_contents("store/sales.json"), true ); (designed to read from json file) is not in your control. Means, in some condition that you can not change the function written there. Then i MUST feed the function with JSON only. Commented Mar 11, 2014 at 3:22

1 Answer 1

2

Actually, json_decode returns a STD object, not an array. To get an array from a JSON string you need to use json_decode($string, true).

3
  • 1
    Well, look at his code: json_decode( file_get_contents("settings/config.json"), true ); Commented Mar 10, 2014 at 14:11
  • Hi Wordpress Developer, i'm not asking about getting Array from JSON. I'm asking about converting PHP Array -> JSON Encoded -> JSON Decode -> PHP Array. Commented Mar 10, 2014 at 14:14
  • You can safely assume that json_encode of an existing array, followed by a json_decode of the resulting string will produce the same array, with one exception: all elements in the input array must be serializable. Commented Mar 10, 2014 at 14:16

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.