David Addoteye David Addoteye - 2 months ago 32
MySQL Question

How to Create Nested JSon With php

How do I create a JSON like this with PHP? I am trying to convert a dynamic data and save it into a MySQL database.

{
"fbd49440-a5a1-48be-b13e-e8efddad3588": {
"0": {
"value": "dsfrasdf5464356dfs hdhfg dfgh"
}
},
"0fc71cea-5609-40a7-a1d2-b78139660f8f": {
"0": {
"value": "50"
}
},
"73936e70-4329-4aba-b47c-42c64ced420c": {
"0": {
"file": "\/components\/com_djclassifieds\/images\/item\/25_juliet_ibrahim.jpg",
"uniqid": "59a352b96773325",
"title": "",
"file2": "",
"overlay_effect": "",
"caption": "",
"width": "",
"height": ""
},
"ac00b95e-9eeb-4035-bf4a-ff206319b2d6": {
"0": {
"value": "members-in-good-standing-2014",
"text": "",
"target": "0",
"custom_title": "",
"rel": ""
}
},
"69072346-fe4c-489e-8e2b-5a7d7409fd44": {
"0": {
"value": "34"
}
}
}


I tried the code below but it did not give me the result I want.

$json = (
"fbd49440-a5a1-48be-b13e-e8efddad3588"=> (
$array1
),
"0fc71cea-5609-40a7-a1d2-b78139660f8f"=> (
$array2
),
"73936e70-4329-4aba-b47c-42c64ced420c"=> (
$array3
),
"ac00b95e-9eeb-4035-bf4a-ff206319b2d6"=> (
$array4
),
"69072346-fe4c-489e-8e2b-5a7d7409fd44"=> (
$array5
)
)

echo json_encode($json);


I will be glad if someone could help me, thank you

Answer Source

json_encode will take multiple types of valid data and try to encode them into a json representation of it. It does require that the input is valid.

The code you have pasted has multiple syntax errors, and we cannot tell what is in your $array1. $array2, $array3, $array4, $array5. However a couple of changes (and assuming your $arrays are actual arrays) has your code working.

There are also bitmask options in the form of JSON Constants that will define how your JSON should be stored.

$array = array( "data" => "value");  // dummy array to show data working

$json = array( //defined the following as an array
    "fbd49440-a5a1-48be-b13e-e8efddad3588"=> array( //notice each of these are now defined as arrays
        $array
    ),
    "0fc71cea-5609-40a7-a1d2-b78139660f8f"=> array(
        $array
    ),
    "73936e70-4329-4aba-b47c-42c64ced420c"=> array(
        $array
    ),
    "ac00b95e-9eeb-4035-bf4a-ff206319b2d6"=> array(
        $array
    ),
    "69072346-fe4c-489e-8e2b-5a7d7409fd44"=> array(
        $array
    )
); //added semicolon to end the declaration

echo json_encode($json, ( JSON_FORCE_OBJECT + JSON_PRETTY_PRINT ) ); 
// added JSON_FORCE_OBJECT and JSON_PRETTY_PRINT 
// As bitmask options, they return a constant to give `json_encode` instructions
// JSON_FORCE_OBJECT => 16, JSON_PRETTY_PRINT => 128 
// JSON_FORCE_OBJECT + JSON_PRETTY_PRINT = 16 + 128 = 144
// json_encode( $array, ( JSON_FORCE_OBJECT + JSON_PRETTY_PRINT ) = json_encode ($array, 144);

Returns

{"fbd49440-a5a1-48be-b13e-e8efddad3588":{"0":{"data":"value"}},"0fc71cea-5609-40a7-a1d2-b78139660f8f":{"0":{"data":"value"}},"73936e70-4329-4aba-b47c-42c64ced420c":{"0":{"data":"value"}},"ac00b95e-9eeb-4035-bf4a-ff206319b2d6":{"0":{"data":"value"}},"69072346-fe4c-489e-8e2b-5a7d7409fd44":{"0":{"data":"value"}}}

Array of Arrays in your json data.

If you want it to print the index of each result, that needs to be treated as an array as well. Simple enough to understand when there are multiple pairs in the result, less intuative when the result is one pair.

$array1 = array( "data" => "value");
echo json_encode($array1, JSON_FORCE_OBJECT );
//would return
//{"data":"value"}

$array2 = array( 
    array( "data" => "value" ),
    array( "data" => "value" )
    );
echo json_encode($array2, JSON_FORCE_OBJECT );
// would return
// {"0":{"data":"value"},"1":{"data":"value"}}

$array3 = array( 
    array( "data" => "value" )
    );
echo json_encode($array3, JSON_FORCE_OBJECT );
// would return
// {"0":{"data":"value"}}