0

How do I convert an 'N' elements single dimensional array to 'N' level nested array in PHP ?

Example:

Input:

$input = array('Orange','Apple','Banana');

Expected Output:

$output = array(
    'name' => 'Banana',
    'sub_category' => array(
         'name' => 'Apple',
         'sub_category' => array(
             'name' => 'Orange'
);

This is my code:

  $categories = array('Orange','Apple','Banana');
  $count = count($categories);
  for($i=0;$i<=$count;$i++){
    if(isset($categories[$i+1])){
      $parent = $categories[$i+1]; // parent      
        $categories[$i+1]=array(
          'name' => $categories[$i+1],
          'sub_category' => array('name' => $categories[$i])
        );
    }   
  }
  $categories = $categories[$count-1];
  var_dump($categories);

My code is sloppy and I also get the following incorrect output:

$output = array(
    'name' => 'Banana',
    'sub_category' => array(
       'name' => array(  
         'name' => 'Apple',
         'sub_category' => array(
             'name' => 'Orange'
       );
);

Edit 1:

The problem/solution provided here does not seem to be answering my question.

6
  • 1
    How do you want to determine multiple sub categories? Is the output supposed to be from the last element being the parent and every subsequent one being a sub category of the previous element? Commented Oct 12, 2017 at 19:10
  • Yes. Correct. The last element is the super parent and the ones preceding it is its sub-category. Commented Oct 12, 2017 at 19:14
  • 2
    You could use simple recursion technique: function toNestedArray(array $input, array $result = []) { $result = ['name' => array_pop($input)]; if (count($input)) { $result['sub_category'] = toNestedArray($input, $result); } return $result; } Commented Oct 12, 2017 at 19:23
  • @jay-blanchard Question sounds similar to the duplicate, but it is not what the OP was asking. Commented Oct 12, 2017 at 19:26
  • 1
    Thanks a lot @x3ns. Your solution works perfectly. Recursion is the key. Commented Oct 12, 2017 at 19:28

3 Answers 3

0

You could use simple recursion technique:

function toNestedArray(array $input, array $result = [])
{
    $result = ['name' => array_pop($input)];
    if (count($input)) {
        $result['sub_category'] = toNestedArray($input, $result);
    }

    return $result;
}
Sign up to request clarification or add additional context in comments.

Comments

0
$categories = array('Orange','Apple','Banana');
  $count = count($categories);
  $categories2=array();
  for($i=$count-1;$i>0;$i--){
      if($i-2>-1){

          $categories2=array(
          'name'=>$categories[$i],
          'sub_category'=>array('name'=>$categories[$i-1],'sub_categories'=>array('name'=>$categories[$i-2]))
          ); 
      }
  }
  echo "<pre>";
  print_r($categories2);
  echo "</pre>";

Comments

0

A simple option is to loop over the $input array, building the $output array from inside to out.

$output = array();
foreach ($input as $name) {
    if (empty($output)) {
        $output = array("name" => $name);
    } else {
        $output = array("name" => $name, "sub_category" => $output);
    }
}

Comments

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.