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 asjson_decode( json_encode($phpArray), true )
, will it give the exact object like i get fromjson_decode("sales.json")
file? - (or) how can i feed the
json_decode
function with a PHPArray()
object i have?
$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.