1

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"
        }]
    }
]
1

2 Answers 2

1

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

7 Comments

Thanks, working fine. how i will add one more child like array $childdirectory = Array ( [0] => stdClass Object ( [child_name] => ABC1 [child_id] => abc1 [sub_id] => toyota [dir_id] => car) [1] => stdClass Object ( [child_name] => ABC2 [child_id] =>abc2 [sub_id] => volvo [dir_id] => bus) )
You will need to loop through this $childdirectory array like the same way you are looping through $subdirectory array.
when am adding one more value in directory folder without adding any child, not getting correctly eg: $directory = Array ( [0] => stdClass Object ( [dir_name] => Car [dir_id] => car ) [1] => stdClass Object ( [dir_name] => Bus [dir_id] => bus ) [2] => stdClass Object ( [dir_name] => Truck [dir_id] => truck))
Updated the answer to handle above case.
like this only getting, adding parent without any child it's not showing the parent value. The new parent added 'Truck' not showing in result [ { id: "car", value: "Car", data: [{ id: "toyota", value: "Toyota" }] }, { id: "bus", value: "Bus", data: [{ id: "volvo", value: "Volvo" }] } ]
|
0

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);
    }

2 Comments

Thanks for your answer, but i tried like this. I am getting this way : [ { id: "car", value: "Car", data: [{ car: "toyota", Car: "Toyota" }] }, { id: "bus", value: "Bus", data: [{ bus: "volvo", Bus: "Volvo" }] } ]
Inside second foreach am done like this: foreach ($subdirectory as $sub) { if ($dir->id == $sub->dir_id) { $row_array['data'][] = array( $row_array['id'] => $sub->sub_id, $row_array['value'] => $sub->sub_name ); } }

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.