Rebase Array Keys After Unsetting Elements

Rebase array keys after unsetting elements

Try this:

$array = array_values($array);

Using array_values()

Make array keys to start from 0 again in php

Use array_values:

$Compare = array_values($Compare);

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.

Reset PHP array index to start from 0 after unset operation

Use array_values function to reset the array, after unset operation.

Note that, this method will work for all the cases, which include unsetting any index key from the array (beginning / middle / end).

array_values() returns all the values from the array and indexes the array numerically.

Try (Rextester DEMO):

$cars = array("Volvo", "BMW", "Toyota", "Mercedes");
unset($cars[0]);
$cars = array_values($cars);
var_dump($cars); // check and display the array

How to re-index the values of an array in PHP?

array_values() will re-index the array.

How to get difference of two arrays without association in php

You're almost there, just one more step: array_values($results)

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.

How to reindex filted Array

I would use array_walk and then array_filter then array_values to reset the index.

For example:

<?php
$array = [
'radios1' => [
'on'
],
'from' => [
'',
'Bangalore',
'',
'',
]
];

array_walk($array, function (&$value, $key) {
$value = array_values(array_filter($value));
});

print_r($array);

https://3v4l.org/Km1i8

Result:

Array
(
[radios1] => Array
(
[0] => on
)

[from] => Array
(
[0] => Bangalore
)

)


Related Topics



Leave a reply



Submit