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
Preserve Key Order (Stable Sort) When Sorting With PHP'S Uasort
How to Find the First and Last Date in a Month Using PHP
Submit an HTML Form With Empty Checkboxes
Sanitizing Strings to Make Them Url and Filename Safe
Elegant Way to Get the Count of Months Between Two Dates
Fastest Way to Convert String to Integer in PHP
PHP: Writing a Simple Removeemoji Function
Update Fee Dynamically Based on Radio Buttons in Woocommerce Checkout
Fatal Error: Cannot Use Object of Type MySQLi_Result
Intl Extension: Installing PHP_Intl.Dll
How to Change Envelope from Address Using PHP Mail
PHP, Getting Variable from Another PHP-File
Sort and Display Directory List Alphabetically Using Opendir() in PHP