Search code examples
phparraysindexingremovechild

PHP remove Item in array and reset key


I have a big array (multidimensional) and I need to remove items in it and reset the index for it. I have no idea how to do it.

Example:

$array = array(
 0 => 'Name G',
 1 => 'Name Z',
 2 => 'Name A',
 3 => 'Name H',
 4 => 'Name U',
)

I need this:

$array = array(
 0 => 'Name G',
 1 => 'Name A',
 2 => 'Name U',
)

I can do the unset() fine .. but I have this:

$array = array(
 0 => 'Name G',
 2 => 'Name A',
 4 => 'Name U',
)

I Can't SORT it

Is there a php function that will do this? else How can I do it?


Solution

  • $array = array(
        0 => 'Name G',
        1 => 'Name Z',
        2 => 'Name A',
        3 => 'Name H',
        4 => 'Name U',
    );
    
    unset($array[1]);
    unset($array[3]);
    $array = array_values($array);