phparraysmultidimensional-arrayarray-splice

PHP Array Splice - insert values with keys in multidimensional array


i've tried so many things but my array would not take the key that is assigned.

Here's the original array

$items = array(
            'main' => array(
                'label'  => ('MENU'),
                'entries'=> array('dash1'=> 
                                    array('label'=>'Dashboard 1',
                                          'icon' => 'fas fa-wallet'),
                                  'dash2'=> 
                                     array('label'=> 'Dashboard 2',
                                           'icon' => 'fas fa-tachometer-alt'),
                                   'dash3'=> 
                                     array('label'=> 'Dashboard 3',
                                           'icon' => 'fas fa-tachometer-alt')
                                  )
                            )
         );

Then i'm introducing a new array

$new_items=array('newdash'=>array('label'=>'New Dashboard',
                                'icon' => 'fas fa-wallet'
                                )
                 ) ;

i want to put this $new_items under dash1, so i used array_splice

$old_items=$items['main']['entries'];
array_splice($old_items,1,0,$new_items);
print_r($old_items);

The output is this

Array ( [dash1] => Array ( [label] => Dashboard 1 [icon] => fas fa-wallet ) 
        [0] => Array ( [label] => New Dashboard [icon] => fas fa-wallet ) 
        [dash2] => Array ( [label] => Dashboard 2 [icon] => fas fa-tachometer-alt )
        [dash3] => Array ( [label] => Dashboard 3 [icon] => fas fa-tachometer-alt ) 
      )

The 0 should be 'newdash' value.

Just FYI, i've also tried these 2 codes below after some researching, but the output still the same. It did not take the newdash value as key.

$new_items=$items['main']['entries'][]=array('newdash'=>array('label'=>'New Dashboard',
                                   'icon' => 'fas fa-wallet')
                   );

and

$new_items=$items['main']['entries']['newdash']=array('label'=>'New Dashboard','icon' => 'fas fa-wallet');

Solution

  • Use the array_slice function to split and then merge arrays:

    function array_insert_assoc($array, $new_item, $after_key = null) {
        if ($after_key === null) {
            // Insert at the beginning of the array
            return array_merge($new_item, $array);
        }
        // Find the index of the array key $after_key
        $index = array_flip(array_keys($array))[$after_key] ?? null;
        if ($index >= 0) {
            return array_merge(
                array_slice($array, 0, $index + 1),
                $new_item,
                array_slice($array, $index + 1)
            );
        }
    }
    
    // Insert as first item
    $items['main']['entries'] = 
        array_insert_assoc($items['main']['entries'], $new_items);
    
    // Insert after 'dash1' key
    $items['main']['entries'] = 
        array_insert_assoc($items['main']['entries'], $new_items, 'dash1');
    
    print_r($items);