After Array_Filter(), How to Reset the Keys to Go in Numerical Order Starting at 0

After array_filter(), how can I reset the keys to go in numerical order starting at 0

If you call array_values on your array, it will be reindexed from zero.

php: array_filter inside loop returns array with different index

Just wrap your array_filter() with array_values(). Let's try like this way-

<?php
$array = [0, 1, 2];
for($i = 0; $i <= 2; $i++){
$filtered = array_values(array_filter($array, function($elem) use ($i){
return ($elem == $i);
}));
var_dump($filtered);
}

DEMO: https://3v4l.org/X1iSF

PHP array_filter get value without key

You can do a couple things.

  1. To get the first element of the array you can use reset($found) https://www.php.net/manual/en/function.reset.php

  2. After filtering the array you can reset the array keys to start at 0 using array_values($found)
    https://www.php.net/manual/en/function.array-values.php

Change array key back in PHP

Try with array_values() after the unset().

$array = array_values($array);

array_filter skip null or 0 value

array_filter will remove all elements having null, empty and 0 values.
To handle value 0 in your case, callback function can be used where you can explicitly specify which values to allow and which to ignore.

I have created a custom callback function that will accept 0 but filter null, empty and false values.

<?php
$url = "log/getLog/0/categorias/5/";

$urlParts = array_filter(explode('/', $url), function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);

$url = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => 0
);

$urlParts = array_filter($url, function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);
?>

Output

Array
(
[0] => log
[1] => getLog
[2] => 0
[3] => categorias
[4] => 5
)
Array
(
[0] => foo
[2] => -1
[5] => 0
)

Working Demo

array_filter skip null or 0 value

array_filter will remove all elements having null, empty and 0 values.
To handle value 0 in your case, callback function can be used where you can explicitly specify which values to allow and which to ignore.

I have created a custom callback function that will accept 0 but filter null, empty and false values.

<?php
$url = "log/getLog/0/categorias/5/";

$urlParts = array_filter(explode('/', $url), function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);

$url = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => 0
);

$urlParts = array_filter($url, function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);
?>

Output

Array
(
[0] => log
[1] => getLog
[2] => 0
[3] => categorias
[4] => 5
)
Array
(
[0] => foo
[2] => -1
[5] => 0
)

Working Demo

How to re-index all subarray elements of a multidimensional array?

To reset the keys of all arrays in an array:

$arr = array_map('array_values', $arr);

In case you just want to reset first-level array keys, use array_values() without array_map.

How do you reindex an array in PHP but with indexes starting from 1?

If you want to re-index starting to zero, simply do the following:

$iZero = array_values($arr);

If you need it to start at one, then use the following:

$iOne = array_combine(range(1, count($arr)), array_values($arr));

Here are the manual pages for the functions used:

  • array_values()
  • array_combine()
  • range()

How to Remove Array Element and Then Re-Index Array?

unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array


Related Topics



Leave a reply



Submit