简体   繁体   中英

How to create nested Json array from associative array in PHP

I'm setting up a file browser and want to parse JSON array to get folder hierarchy, but not able to make the structure I want in JSON array from the associative array.

This is the two associative array which I am fetched from the database.

$directory  = Array ( [0] => stdClass Object ( [dir_name] => Car [dir_id] => car ) [1] => stdClass Object ( [dir_name] => Bus [dir_id] => bus ) )

$subdirectory = Array ( [0] => stdClass Object ( [sub_name] => Toyota [sub_id] => toyota [dir_id] => car) [1] => stdClass Object ( [sub_name] => Volvo [sub_id] => volvo [dir_id] => bus) )

I am tried like this

$parentdirectory = [];
$parentfolder = [];
$subf = [];
$subfolder = [];

foreach ($directory  as $dir) {
  $parentdirectory['id'] = $dir->id;
  $parentdirectory['value'] = $dir->name;
  foreach (subdirectory as $sub) {
    if ($dir->dir_id == $sub->dir_id) {
      $subfolder['id'] = $sub->sub_id;
      $subfolder['value'] = $sub->sub_name;
      array_push($subf, $subfolder);
      array_push($parentdirectory, $subf);
    }
  }
  array_push($parentfolder, $parentdirectory);
}

echo json_encode($parentfolder);

Actually, I want JSON array like this

[
    {
        id: "car",
        value: "Car",
        data: [{
            id: "toyota",
            value: "Toyota"
        }]
    },
    {
        id: "bus",
        value: "Bus",
        data: [{
            id: "volvo",
            value: "Volvo"
        }]
    }
]

Your foreach loop inside foreach loop is inefficient. First create the data array by looping through $subdirectory and then loop through the $directory to get the actual format.

$dataArray = array();
$parentfolder = array();

foreach( $subdirectory as $sub ) {
    $dirName = $sub->dir_id;
    if( !isset( $dataArray[$dirName] ) ) {
        $dataArray[$dirName] = array();
    }
    $dataArray[$dirName][] = array('id' => $sub->sub_id, 'value' => $sub->sub_name);
}

foreach ($directory  as $dir ) {
    $dirName = $dir->dir_id;
    if( isset ( $dataArray[$dirName] ) ) {
        $data = $dataArray[$dirName];
    } else {
        $data = array();
    }
    $parentfolder[] = array('id' => $dir->dir_id, 'value' => $dir->dir_name, 'data' => $data);
}

echo json_encode($parentfolder);

I think you should set data as an array first, like this:

$parentfolder= array();
    foreach ($directory  as $dir) {
        $row_array = array();
        $row_array["id"] = $dir[id];
        $row_array["data"] = array();
            foreach (subdirectory as $sub) {
                if ($dir["id"] == $sub["dir_id"]) {
                    $row_array["data"][] = array(
                        //and so on..
                    );
                }
            }
        array_push($parentfolder, $row_array);
    }

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