Sure, a null valued element is still considered a valid array element!
For example:
<?php
$arr = [null, null, null];
echo 'Count: ' . count($arr); //Will print 3
In your code, the value of the third element is null, there is no problem with that, no mistery. You are not removing the element, but assigning it a value: null.
Here you got an idea: iterate over the array and remove elements valued null:
$aux = [];
foreach ($arr as $item) {
if (!is_null($item)) {
$aux[] = $item;
}
}
$arr = $aux; //Now $arr has no null elements
Or simply iterate to count not null elements.
$c = 0;
foreach ($arr as $item) {
if (!is_null($item)) {
$c++;
}
}
echo 'Count: ' . $c; //Count without null elements
Or you can build your array adding or not the conditional element. This can be the better solution:
$arr =
[
[
'slug' => 'products-services-pricing',
'text' => 'Products/Services and Pricing',
],
[
'slug' => 'promotions-plan',
'text' => 'Promotions Plan',
],
];
if (1 == 2) {
$arr[] = [
'slug' => 'distribution-plan',
'text' => 'Distribution Plan',
];
}
echo 'Count: ' . count($arr); //Will print 2
null, there is no problem with that, no mistery. You are not removing the element, but assigning it a value:null. Here you got an idea: iterate over the array and remove elements valuednull. Or simply iterate to count notnullelements.