Question

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?

Was it helpful?

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);

OTHER TIPS

you asked 2 questions here i think. either way, if you are trying to re-index the keys use array_values

 $new_array = array_values($array);
 print_r($new_array);

result:

 Array
 (
[0] => Name G
[1] => Name A
[2] => Name U
 )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top