Lets say I have an array like this:
$Array = array(1, 5, 7, 11, 16, 20);
I would like to get this result:
1 - - - 5 - 7 - - - 11 - - - - 16 - - - 20
Basically I’d like to get a "-" for non-existent values in the array.
How can I achieve this outcome?
Thanks for your help.
>Solution :
You can loop through all the numbers you’re interested in and check whether they are in your array:
<pre>
<?php
$array = [1, 5, 7, 11, 16, 20];
for($i = 1; $i <= end($array); $i++) {
if(in_array($i, $array))
print $i . ($i==end($array) ? "" : " ");
else
echo "- ";
};
?>
</pre>