array_diff() leaving empty array keys
-
30-05-2021 - |
Question
Maybe I'm missing something here, but array_diff()
seems to be leaving me empty kesys at the end of an array.
With this example data -
$term_ids = array(0 => 242, 1 => 270);
$term_ids = array_diff($term_ids, array(242, 243, 266, 267, 268, 269));
I am being left with this -
Array
(
[1] => 270
[0] =>
)
From what I understand, this is incorrect ([0] should be unset, leaving just [1]). I am using $term_ids = array_values($term_ids);
after to make the keys correct, but it seems that this should not be necessary, so I'm wondering if someone who knows more than me could clarify?
Solution
I tried
<?php
$arr1 = array(15, 16, 17,18);
$arr2 = array(15, 17);
$arr3 = array_diff($arr1, $arr2);
var_dump($arr3);
?>
and I got [1] => 16, [3] => 18.
This actually seems to be the intended result as the example on the offical page shows.
OTHER TIPS
Unable to reproduce:
$term_ids = array(0 => 242, 1 => 270);
$term_ids = array_diff($term_ids, array(242, 243, 266, 267, 268, 269));
print_r($term_ids);
Output:
Array
(
[1] => 270
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow