23

How do you truncate a PHP array in a most effective way?

Should I use array_splice?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • What do you want to achieve ? Provide some example. – hsz Nov 02 '11 at 18:26
  • 6
    Why the aversion to `array_splice`? – Clive Nov 02 '11 at 18:26
  • I read an SQL join result and need to reset an associative array every 3rd iteration. I thought count could be used here, like in some languages where you set array length to 0... – Alexander Farber Nov 02 '11 at 18:34
  • PHP's array length is just metadata. It's basically read-only. To affect it, you have to actually add/remove elements. – Marc B Nov 02 '11 at 18:58
  • I must **really** be missing the point of this question. If you want to set the length to 0, wouldn't it just be `$result=array();` – TecBrat May 14 '14 at 15:39
  • No, because `$result=array();` would a) allocate a new array b) drop for garbage collection the old array. So it wouldn't be an efficient way to truncate a PHP-array. – Alexander Farber Sep 09 '14 at 10:32
  • Possible duplicate of [PHP: how to 'cut' my array?](http://stackoverflow.com/questions/3585966/php-how-to-cut-my-array) – Organic Advocate Nov 18 '16 at 21:12

4 Answers4

23

You can use the native functions to remove array elements:

  • array_pop - Pop the element off the end of array
  • array_shift - Shift an element off the beginning of array
  • array_slice - Extract a slice of the array
  • unset - Remove one element from array

With this knowledge make your own function

function array_truncate(array $array, $left, $right) {
    $array = array_slice($array, $left, count($array) - $left);
    $array = array_slice($array, 0, count($array) - $right);
    return $array;
}

Demo - http://codepad.viper-7.com/JVAs0a

Peter
  • 16,453
  • 8
  • 51
  • 77
4

Yes, unless you want to loop over the array and unset() the unwanted elements.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Note, in a foreach you would have to use the full array index reference since foreach uses a copy of the array. `foreach($array as $key => $value ) { unset($array[$key] }` not `foreach($array as $key => $value ) { unset($value) }`. The second one just unsets the copy. – Buttle Butkus Apr 04 '14 at 02:46
2

This function should work

function truncateArray($truncateAt, $arr) {
    array_splice($arr, $truncateAt, (count($arr) - $truncateAt));
    return $arr;
}
Bhargav
  • 8,118
  • 6
  • 40
  • 63
uglypointer
  • 377
  • 1
  • 14
0

You can use one of this functions:

function array_truncate(&$arr)
{
    while(count($arr) > 0) array_pop($arr);
}
// OR (faster)
function array_truncate2(&$arr)
{
    array_splice($arr, 0, count($arr));
}

Usage:

$data2 = array("John" => "Doe", "Alice" => "Bob");
array_truncate($data2);
// OR
array_truncate2($data2);
Devtronic
  • 39
  • 7
  • The `while`-loop would probably be the most inefficient way to truncate an array :-) – Alexander Farber May 04 '14 at 09:53
  • @AlexanderFarber of course it is, that's why i said (in the comment) that the other is faster. I included to array_pop method to show a method where you have the option to do something to the element before discarding it. – Rich Schonthal Nov 18 '15 at 21:45