I have this array, I am trying to store the first element(15) in one array(xAxis) and from second element(42) to fifth(23) in another array(yAxis) and again I want to store the sixth element(15) in the array - xAxis and later 4 elements in yAxis. I have more than hundred elements in the source array and want to follow this pattern to store in the arrays.
Array
(
[0] => 15
[1] => 42
[2] => 55
[3] => 42
[4] => 23
[5] => 15
[6] => 38
[7] => 40
[8] => 53
[9] => 10
[10] => 15
)
Thanks.
Use loops :
$array = Array
(
[0] => 15
[1] => 42
[2] => 55
[3] => 42
[4] => 23
[5] => 15
[6] => 38
[7] => 40
[8] => 53
[9] => 10
[10] => 15
)
$x = {};
$y = {};
$step = 0;
for ($i=0; $i < count($array); $i = $i){
if ($step == 0){
array_push($x, $array[$i]);
$i = $i + 1;
}
else
{
array_push($y, $array[$i]);
array_push($y, $array[$i+1]);
array_push($y, $array[$i+2]);
array_push($y, $array[$i+3]);
$i = $i + 4;
}
}
Dont forget to be sure that these steps 1-2-3-4 are possible. that your array is like 10/15/20 long!
else php will return a (silecent) error.
Split the array to parts of 5 items. Then put them in new arrays
$yAxis = array();
foreach(array_chunk($arr,5) as $chunk) {
$xAxis[] = array_shift($chunk);
$yAxis = array_merge($yAxis, $chunk);
}