I am trying to get a foreach loop to display all the values of an array but for some reason it skips value 4.
echo $sku."<br>";
$skuArray = explode(" ", $sku);
$skuCount = array_count_values($skuArray);
print_r($skuArray);
echo "<br><br>";
$i=0;
foreach ($skuCount as $key => $val) {
echo "[".$i."] => ".$key." ";
$i++;
}
and this is what the output looks like:
1DALI0SPBA775RW 2 $92.99 1GJESSGRIP10000 2 $9.99
Array ( [0] => 1DALI0SPBA775RW
[1] => 2
[2] => $92.99
[3] => 1GJESSGRIP10000
[4] => 2
[5] => $9.99 )
[0] => 1DALI0SPBA775RW
[1] => 2
[2] => $92.99
[3] => 1GJESSGRIP10000
[4] => $9.99
As you can see, the foreach loop says that 4 is equal to $9.99 but in the print_r array it is equal to 2 - which is what I expect it to be.
array_count_values
sums the number of times a particular element appears in the array. – Explosion Pills May 15 '12 at 18:01