0

I have array in this structure, and I want delete item[6] from array.

I used unset($arrayname[6]) in php but it's not working. Any help? Is there any way I can remove element [6] from array?

 array(2) {
            [6]=>
            array(2) {
              ["id"]=>
              string(1) "1"
              ["day"]=>
              string(6) "Monday"
            }
            [7]=>
            array(2) {
              ["id"]=>
              string(1) "2"
              ["day"]=>
              string(7) "Tuesday"

            }}
Nikhil
  • 6,493
  • 10
  • 31
  • 68
k_a
  • 1
  • 1
  • 2

2 Answers2

0

You can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically.

$array2 = {{array of array}}
// remove item at index 6 which is 'for' 
unset($array2[6]); 
// Print modified array 
var_dump($array2); 
// Re-index the array elements 
$arr2 = array_values($array2); 
//Print re-indexed array 
var_dump($array2);

This solution will work, please check and let me know.

Shak Shared
  • 53
  • 2
  • 4
Manoj Agrawal
  • 429
  • 3
  • 10
0

This works as you would expect:

<?php
$days =
    array (
        6 => 
        array (
            'id' => 1,
            'day' => 'Mon',
        ),
        7 => 
        array (
            'id' => 2,
            'day' => 'Tue',
        )
    );

unset($days[6]);
var_export($days);

Output:

array (
  7 => 
  array (
    'id' => 2,
    'day' => 'Tue',
  ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25