简体   繁体   中英

Convert PHP array to JSON using multi dimentional arrays

I am trying to convert this php array to a json. This is my code:

$c = array();
$c = array(
$c['cronjobs'] = array(
    'id'=>1189515,
    'groupId'=>12379,
    ),
);
$json = json_encode($c);
echo $json;

This is the output I'd like to acieve: {"cronjobs":[{"id":1186437,"groupId":12379]}

Though using the above code this is what I am getting [{"id":1189515,"groupId":12379}]

The [{"cronjobs" part is not appearing.

I'm not sure what I'm doing wrong.

This should get the result that you want (just wrap an array around the id, groupId array):

<?php
     $c = array();
     $c['cronjobs'] = array(array(
         'id'=>1189515,
         'groupId'=>12379,
     ));

     echo json_encode($c);
     // result {"cronjobs":[{"id":1189515,"groupId":12379}]}
?>

This is how you need to format your array:

$c = array(); // declare the array
$c['cronjobs'] = array( // populate the array
    'id'=>1189515,
    'groupId'=>12379,
);
$json = json_encode($c); // json_encode it
echo $json;

There is no need for $c = array($c['cronjob']); (which was what you were doing).

I think this is what you're looking for:

$c = array('cronjobs' => array());

$c['cronjobs'][] = array('id' => 1189515, 'groupId' => 12379);
//$c['cronjobs'][] = array('id' => 1234, 'groupId' => 4321);

$json = json_encode($c);
echo $json;

Cronjobs needs to contain an array of cronjob objects/arrays

Could also be written using one statement, like this:

$c = array('cronjobs' => array(
    array('id' => 1189515, 'groupId' => 12379),
    array('id' => 1234, 'groupId' => 4321)
));

Your problem is that in PHP array is used to represent both JSON objects and JSON lists hence the confusion. Consider the following code:

$cronjob = array(
    'id' => 1189515,
    'groupId' => 12379
);
echo json_encode($cronjob);
// {"id":1189515,"groupID":12379"}

As you can see this represents a single object. So we'll create a list of objects:

$cronjobs = array($cronjob);
echo json_encode($cronjobs);
// [{"id":1189515,"groupID":12379"}]

This is now a list as expected. Now the parent object:

$c = array(
    'cronjobs' => $cronjobs
);
echo json_encode($c);
// {"cronjobs":[{"id":1189515,"groupID":12379"}]}

In JSON there is a name: value pair system

 { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" } ], "children": [], "spouse": null } 

if you want to achieve like {"cronjobs":[{"id":1186437,"groupId":12379]} then the array must be named like the following in PHP :

$c['cronjobs'] = array(
'id'=>1189515,
'groupId'=>12379,
);

$json = json_encode($c);
echo $json;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM