How to Re-Index the Values of an Array in PHP

PHP reindex array?

Use array_values.

$myarray = array_values($myarray);

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()

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
)

How to reindex an array?

Use array_values:

$reindexed_array = array_values($old_array);

How to re-index arrays?

Just try with array_values:

$newArray = array_values($oldArray);

Re-index numeric array keys


$your_new_array = array_values($your_old_array);

reindex an array using value from multidimensional array

Yes there is a function for that array_column, which lets you extract a specific element from a multi-dimensional array and/or index the result by a specific element. So do both:

$result = array_column($array, 'qAVG', 'mID');

If the sub-arrays have more elements and you want them as well, then just re-index:

$result = array_column($array, null, 'mID');

How to filter out blank values from an array in PHP & Reindex the array

Replace your code from this line => $var = explode(',', $sizes); & add this:

$var = array_filter(explode(',', $sizes));
$reindexed = array();

foreach ($var as $row)
{
if ($row !== null)
$reindexed[] = $row;
}

print_r($reindexed);
exit();

Let's See The Explaination of the code now

1.) This is 1st reference link from where i took the idea to filer but ass you prnt the array you will see that the array indexes are jumbled => Remove empty array elements

$var = array_filter(explode(',', $sizes));
$reindexed = array();

so we create a new variables reindexed as an array to store the reindexed array value

2.) To remove the jumbled array index and reindex the array i took refernce from this link => How to remove null values from an array?

$reindexed = array();

foreach ($var as $row)
{
if ($row !== null)
$reindexed[] = $row;
}

print_r($reindexed);

Reindex Array values

First, you can convert your values to a array, as the input is a json. Then parse the array, to remove the required value.
After parsing the array, you can convert it back to json (if it is needed).
The next script remove the value indicated in the $valueToRemove variable.

<?php
$jsonValues = '{
"sequence": 2,
"data": {
"1": {"sequence": "1", "todo": "dummy todo"},
"2": {"sequence": "2", "todo": "dummy todo 2"},
"3": {"sequence": "3", "todo": "ssdummy tossssdo 3"}
}
}';
$valueToRemove = 2;
$arrayValues = json_decode($jsonValues, true);
$oldData = $arrayValues['data'];
$newData = array();
$counter = 0;
foreach ($oldData as $oldIndex => $oldValue) {
if ($valueToRemove != $oldIndex) {
$counter++;
$newData[$counter] = array(
'sequence' => $counter,
'todo' => $oldValue['todo']
);
}
}
$arrayValues['data'] = $newData;
$jsonValues = json_encode($arrayValues);
var_dump($jsonValues);
?>

I have the following output from the script:

{"sequence":2,"data":{"1":{"sequence":1,"todo":"dummy todo"},"2":{"sequence":2,"todo":"ssdummy tossssdo 3"}}}

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

array_values() will re-index the array.



Related Topics



Leave a reply



Submit