Array_diff and renumber the number keys

(I'm a beginner)

My script uses standard

$c = 0;
$t = count($array);

while ($c < $t) {
  $blah = $array[$c];
  ++$c;
}

pretty wide. But I just ran into a situation where I would also need array_diff, and this breaks it all to hell, because now the number keys have spaces. I get errors Undefined offseteverywhere.

How to reset the numeric keys of an array? The order of the objects in the array does not matter.

+5
source share
3 answers

To reset the keys, you can use array_values():

$array = array_values($array);
+25
source

You do not need to reset the keys of your array: you must change the way you pass through it.

while foreach, :

foreach ($array as $key => $value) {
    // $key contains the index of the current element
    // $value contains the value of the current element
}
+7

Thanks, Tattoo.

For lulz, I will share with you the following idiotic hack that I used, expecting a reasonable answer:

$badArray = array_diff($allData, $myData);

$string = implode(",",$badArray);

$dump = explode(",",$string);

$goodArray = $dump;

worked. It made me feel dirty, but it worked.

+2
source

All Articles