PHP | Remove element from array with reordering?
Asked Answered
C

7

9

How can I remove an element of an array, and reorder afterwards, without having an empty element in the array?

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]); // will distort the array.
?>

Answer / Solution: array array_values ( array $input ).

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]);
   print_r(array_values($c));
   // will print: the array cleared
?>
Chelate answered 31/1, 2010 at 19:14 Comment(0)
M
16
array_values($c)

will return a new array with just the values, indexed linearly.

Microtone answered 31/1, 2010 at 19:16 Comment(0)
M
4

If you are always removing the first element, then use array_shift() instead of unset().

Otherwise, you should be able to use something like $a = array_values($a).

Menadione answered 31/1, 2010 at 19:17 Comment(0)
M
2

Another option would be array_splice(). This reorders numeric keys and appears to be a faster method if you are crunching enough data to care. But I like unset() array_values() for readability.

array_splice( $array, $index, $num_elements_to_remove);

http://php.net/manual/en/function.array-splice.php

Speed test:

    ArraySplice process used 7468 ms for its computations
    ArraySplice spent 918 ms in system calls
    UnsetReorder process used 9963 ms for its computations
    UnsetReorder spent 31 ms in system calls

Test Code:

function rutime($ru, $rus, $index) {
    return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
}

function time_output($title, $rustart, $ru) {
        echo $title . " process used " . rutime($ru, $rustart, "utime") .
            " ms for its computations\n";
        echo $title . " spent " . rutime($ru, $rustart, "stime") .
            " ms in system calls\n";
}

$test = array();
for($i = 0; $i<100000; $i++){
        $test[$i] = $i;
}

$rustart = getrusage();
for ($i = 0; $i<1000; $i++){
        array_splice($test,90000,1);
}
$ru = getrusage();
time_output('ArraySplice', $rustart, $ru);

unset($test);
$test = array();
for($i = 0; $i<100000; $i++){
        $test[$i] = $i;
}

$rustart = getrusage();
for ($i = 0; $i<1000; $i++){
        unset($test[90000]);
        $test = array_values($test);
}
$ru = getrusage();
time_output('UnsetReorder', $rustart, $ru);
Mamoun answered 21/2, 2013 at 19:57 Comment(0)
R
1

If you only remove the first item of the array, you could use array_shift($c);

Reveal answered 31/1, 2010 at 19:17 Comment(0)
B
0

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.

array_shift($stack);

example:

$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);

Output:

Array
(
    [0] => banana
    [1] => apple
    [2] => raspberry
)

Source: http://php.net/manual/en/function.array-shift.php

Barbaraanne answered 18/12, 2017 at 9:34 Comment(1)
Wow, you're big on copying and pasting. Please read stackoverflow.com/help/how-to-answerTench
C
0
$array=["one"=>1,"two"=>2,"three"=>3];
$newArray=array_shift($array);

return array_values($newArray);

return [2,3] array_shift remove first element from an array array_values return just values

Crewel answered 13/8, 2019 at 14:44 Comment(0)
S
-1

Or reset(); is also a good choice

Sestos answered 31/1, 2010 at 19:18 Comment(1)
reset(); is not, according to PHP.net: "reset() rewinds array 's internal pointer to the first element and returns the value of the first array element."Reveal

© 2022 - 2024 — McMap. All rights reserved.