我想转换这个数组:
Array
(
[5] => 3.5
[6] => 4.5
[7] => 5.5
)
Array
(
[8] => 5
[9] => 6
[10] => 7
)
进入一个看起来像这样的二维数组:
$array = array(
"a" => array("3.5","4.5", etc.),
"b" => array("5","6", etc.),
);
我想我已接近以下代码,但我仍然缺少一个名称,如" a"和" b"对于每个阵列组:
$array = array($result);
echo '<pre>';
print_r(array_chunk($array,2, true));
echo '</pre>';
以下是我获取两个数组的代码:
$period = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$sma = array(6,9);
foreach ($sma as $range) {
$sum = array_sum(array_slice($period, 0, $range));
$result = array($range - 1 => $sum / $range);
for ($i = $range, $n = count($period); $i != $n; ++$i) {
$result[$i] = $result[$i - 1] + ($period[$i] - $period[$i - $range]) / $range;
}
$array = array($result);
echo '<pre>';
print_r(array_chunk($array,2, true));
echo '</pre>';
}
答案 0 :(得分:1)
$period = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
$sma = array(6,9);
$array = array();
foreach ($sma as $range) {
$sum = array_sum(array_slice($period, 0, $range));
$result = array($range - 1 => $sum / $range);
for ($i = $range, $n = count($period); $i != $n; ++$i) {
$result[$i] = $result[$i - 1] + ($period[$i] - $period[$i - $range]) / $range;
}
//add each array to final array
$array[] = $result;
}
echo '<pre>';
print_r($array);
echo '</pre>';