Reset PHP Array Index

Reset PHP Array Index

The array_values() function [docs] does that:

$a = array(
3 => "Hello",
7 => "Moo",
45 => "America"
);
$b = array_values($a);
print_r($b);
Array
(
[0] => Hello
[1] => Moo
[2] => America
)

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 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 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 to Remove Array Element and Then Re-Index Array?

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

How to reset array keys in PHP

Just use array_values:

<?php
$a = ['three', 'two', 'one'];
$b = ['five', 'one'];

$output = array_values(array_intersect($a, $b));

print_r($output);

Example here

How to reset array index after unset key in foreach loop in php?

key emp_id is as string, not as index.
your code has unset emp_id.
alternatively, you may use array_map function and then use array_values to reindex your array.

Efficiency of using foreach loops to clear a PHP array's values

Like Zack said in the comments below you are able to simply re-instantiate it using

$foo = array(); // $foo is still here

If you want something more powerful use unset since it also will clear $foo from the symbol table, if you need the array later on just instantiate it again.

unset($foo); // $foo is gone
$foo = array(); // $foo is here again

If we are talking about very large tables I'd probably recommend

$foo = null; 
unset($foo);

since that also would clear the memory a bit better. That behavior (GC) is however not very constant and may change over PHP versions. Bear in mind that re-instantiating a structure is not the same as emptying it.

How can I reset array key and shift first key to 1 in php?

Please try following code.

function reindex ($arr, $start_index)
{
$arr = array_combine(range($start_index, count($arr) + ($start_index - 1)), array_values($arr));
return $arr;
}

Following is my test result.

$arr = array ( 
0 => 'apple',
1 => 'banana',
3 => 'orange',
4 => 'melon'
);

$arr = reindex ($arr, 1);

foreach( $arr as $key => $value) {
echo "Index: ".$key." Value: ".$value."\n";
}

And following is the output.

Index: 1 Value: apple
Index: 2 Value: banana
Index: 3 Value: orange
Index: 4 Value: melon

You can test this code on following online tester.

http://sandbox.onlinephpfunctions.com/



Related Topics



Leave a reply



Submit