In the following code I have two arrays $x and $m. I then mapped the two arrays as $k but what I want is that after the two arrays are mapped, if there is an empty element on either of the array $x and $m ($x in this case) then it needs to filter out the corresponding mapped elements from $k as well which is the mapped array.
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("apple", "", 2, null, -5, "orange", 10, false, "",22);
var_dump(array_filter($x));
echo "<br/>";
$m = [12,13,14,15,16,17,18,19,20,21];
$k = array_map(null, $x, $m);
array_filter($k);
shuffle($k);
$count = 1;
foreach ($k as $a) {
if($count <= 8){
echo "The number is: $a[0]. $a[1]. <br>";
}else{
break;
}
$count++;
}
?>
</body>
</html>
But this doesn't seem to work. The output I get is the following:
array(6) { [0]=> string(5) "apple" [2]=> int(2) [4]=> int(-5) [5]=> string(6) "orange" [6]=> int(10) [9]=> int(22) }
The number is: . 19.
The number is: -5. 16.
The number is: apple. 12.
The number is: 22. 21.
The number is: . 20.
The number is: . 15.
The number is: . 13.
The number is: 2. 14.
What I want is that . 20 or . 15 etc be filtered out. How do I go about solving this?
array_filterreturns a new, filtered array. It doesn't modify the original array. Use it akin toarray_map, not akin toshuffle.