0

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

1
  • In future, you should post the result you are getting. Often the incorrect result will give information that will help locate the issue. In this case, the incorrect result would say "Syntax error" because there are multiple syntax issues with your code. Commented Aug 28, 2017 at 5:59

1 Answer 1

1

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"}}
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you @Luke for your answer. It is supposed to list the arrays with their index value like so "0": { "value": "1dsfrasdf5464356dfs hdhfg dfgh" }, "1": { "value": "2dsfrasdf5464356dfs hdhfg dfgh" }, "2": { "value": "3dsfrasdf5464356dfs hdhfg dfgh" }
Ah, that's the JSON being represented as an object. I'll adjust the answer to suit. Your arrays should be included as an array of arrays (You'll see the difference when I change it) and add JSON_FORCE_OBJECT as a second parameter to the json_encode()
Of course, your $array1 etc. could be an array of results. In which case you would not need to define them as such in each line.
My updated answer has that. I'll add detail for clarity
That's JSON_PRETTY_PRINT, another bitmask option. Details added to answer
|

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.