I have worked hard this night to figure out how to split my arrays into a lot of arrays, defined by a value in the array
I could have this value
$array = array (
array ("Apple", 10),
array("Ball", 5)
);
Then I want Apple to have 10 arrays where the value is “Apple”, and Ball to have 5 arrays where the value is “Ball”
Then I came up with this, but the output seems pretty strange..
$newarray = array();
foreach($array as $val):
for($i = 1; $i <= $val[1]; $i++):
$newarray[$i] = $val[0];
endfor;
endforeach;
print_r($newarray);
// Array ( [0] => Ball [1] => Ball [2] => Ball [3] => Ball [4] => Ball [5] => Ball [6] => Apple [7] => Apple [8] => Apple [9] => Apple [10] => Apple )
Hope you guys understand my question, and hope some one can figure it out.
In advance, thanks.
array_fill()can help here:See it here in action: http://viper-7.com/MhnWFt