Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php Insert data in multidimensional array by keys

I'm trying to insert data into a multidimensional array but it get's me struggled. I'm unable to do it. It's so confusing for me.

I have a "tree" array:

$tree = array(
               10 => array(),
               11 => array(
                         4 => array(),
                         5 => array(),
                         6 => array()
               )
        );

And the array of the path I have to use to insert the data:

$path = array(11,5);

The result should be:

$tree = array(
               10 => array(),
               11 => array(
                         4 => array(),
                         5 => array($data),
                         6 => array()
               )
        );

This must work with any multidmensional array (n-dimensional).

As a note, the insertion will always take place in one of the deepest branches of the tree. For example, if the tree is a three-dimensional array, the path variable will have 3 values for sure and the insertion will be in one of the n three-dimensional branches the array could have.

I would type here what I have done but is not so much. I event don't know if I should choose a recursive function to do it or other way.

Thanks in advance.

like image 274
cooper Avatar asked Mar 16 '26 16:03

cooper


2 Answers

Well, you just need a recursive function and work with the array passing by ref, something like...

$tree = [
    10 => [],
    11 => [
        4 => [],
        5 => [],
        6 => []
    ]
];

$path = [
    11,
    5,
    0
];

var_dump(insertIntoArr($tree, 'Awesome value', $path, $tree));

function insertIntoArr(&$chunk, $data, $path, &$original) {
    $key = array_shift($path);

    if (!empty($path)) {
        if (!isset($chunk[$key])) {
            throw new \Exception('OMG! This path does\'t exists, I can\'t continue...');
        }

        return insertIntoArr($chunk[$key], $data, $path, $original);
    }

    $chunk[$key] = $data;

    return $original;
}

Prints...

array(2) {
  [10]=>
  array(0) {
  }
  [11]=>
  array(3) {
    [4]=>
    array(0) {
    }
    [5]=>
    array(1) {
      [0]=>
      string(13) "Awesome value"
    }
    [6]=>
    array(0) {
    }
  }
}
like image 96
kiske Avatar answered Mar 18 '26 07:03

kiske


Method 1

You can use pass by reference with foreach

$tree = [
       10 => [],
       11 => [
                4 => [],
                5 => [],
                6 => []
       ]
    ];
$path = array(11,5,0);
$inseration = array('A','B');
$current = &$tree;
foreach($path as $key){
  $current = &$current[$key];
}
$current = $inseration;
echo '<pre>';
print_r($tree);

DEMO

Method 2

By using function

$tree = [
       10 => [],
       11 => [
                4 => [],
                5 => 'r',
                6 => []
       ]
    ];
$path = array(11,5);
$inseration = [1,2];
insertByKey($tree, $path, $inseration);

function insertByKey(&$array, $path, $inseration){
  $current = &$array;
  $key = array_shift($path);
  while($key > 0){
    $current = &$current[$key];
    $key = array_shift($path);
  }
  $current = $inseration;
}
like image 32
Rakesh Jakhar Avatar answered Mar 18 '26 05:03

Rakesh Jakhar