在多维数组中添加数字

时间:2014-10-06 15:48:48

标签: php arrays multidimensional-array

是否有更快的方法在多维数组的每个片段中添加所有数字而不是仅手动完成所有数字?之前我被告知可以使用foreach循环,但我已经碰到了一堵砖墙。

我正努力让它显示每个校区的课程总数,然后是每个课程的学生总数。

我觉得答案是盯着我,但我不确定。

<?

$campus = array();

$campus[1]['course1'] = 5;   // <---- Number enrolled
$campus[1]['course2'] = 15;
$campus[1]['course3'] = 22;
$campus[1]['course4'] = 21;
$campus[1]['course5'] = 12;
$campus[1]['course6'] = 25;
$campus[1]['course7'] = 16;
$campus[1]['course8'] = 11;
$campus[1]['course9'] = 17;
$campus[1]['course10'] = 23;

$campus[2]['course1'] = 11;
$campus[2]['course2'] = 23;
$campus[2]['course3'] = 51;
$campus[2]['course4'] = 25;
$campus[2]['course5'] = 32;
$campus[2]['course6'] = 35;
$campus[2]['course7'] = 32;
$campus[2]['course8'] = 52;
$campus[2]['course9'] = 25;
$campus[2]['course10'] = 21;

$campus[3]['course1'] = 2;
$campus[3]['course2'] = 12;
$campus[3]['course3'] = 32;
$campus[3]['course4'] = 32;
$campus[3]['course5'] = 25;
$campus[3]['course6'] = 26;
$campus[3]['course7'] = 29;
$campus[3]['course8'] = 12;
$campus[3]['course9'] = 15;
$campus[3]['course10'] = 11;

echo "<pre>";
print_r($campus);
echo "<br/>";

foreach($campus as $key=>$value)
{

}

5 个答案:

答案 0 :(得分:1)

使用array_sum()在数组中添加数字,并使用array_map()将其应用于$campus数组的每个元素。

$total_by_campus = array_map('array_sum', $campus);

答案 1 :(得分:0)

$courses = array();
foreach($campus as $key=>$value)
{
    foreach($value as $course=>$num)
        $courses[$course] += $num;
}
var_dump($courses);

应该这样做。

答案 2 :(得分:0)

你可以使用PHP的array_sum()函数来累加你给它的数组的值。

<?php

$sum = array_sum($campus[1]);
echo $sum;

答案 3 :(得分:0)

$totcount = 0;
$count  = array();
foreach($campus as $key=>$value)
{
    foreach($value as $value1 => $value2)
    {
        $count[$value1]+=$value2;
        $totcount++;
    }
}

print_r($count);
echo "<br><br><br>". $totcount;

答案 4 :(得分:0)

看看:

$campus = array
(
    '1' => array
    (
        'course1'  => 5,
        'course2'  => 15,
        'course3'  => 22,
        'course4'  => 21,
        'course5'  => 12,
        'course6'  => 25,
        'course7'  => 16,
        'course8'  => 11,
        'course9'  => 17,
        'course10' => 23,
    ),
    '2' => array
    (
        'course1'  => 11,
        'course2'  => 23,
        'course3'  => 51,
        'course4'  => 25,
        'course5'  => 32,
        'course6'  => 35,
        'course7'  => 32,
        'course8'  => 52,
        'course9'  => 25,
        'course10' => 21,
    ),
    '3' => array
    (
        'course1'  => 2,
        'course2'  => 12,
        'course3'  => 32,
        'course4'  => 32,
        'course5'  => 25,
        'course6'  => 26,
        'course7'  => 29,
        'course8'  => 12,
        'course9'  => 15,
        'course10' => 11,
    ),
);

foreach ($campus as $key0 => $value0)
{
    // $key0 == (1, 2, 3)
    // $value0 == array(course1 => 5, course2 => 15, course3 => 22, ...)
    foreach ($value0 as $key1 => $value1)
    {
        // $key1 == (course1, course2, course3, ...)
        // $value1 == (5, 15, 22, ...)
    }
}

抱歉,您发布了一个问题:&#34;如何查看阵列的内容以查看他们是否有任何号码?&#34;

<?php

$cowboyfile = "COWBOY.TXT";

$data = array();
$data[] = "Colt Peacemaker, 12.20";
$data[] = "Holster, 2.00";
$data[] = "Levi Strauss Jeans, 1.35";
$data[] = "Saddle, 40.00";
$data[] = "Stetson, 10.00";
// Writing in the File
file_put_contents($cowboyfile, implode("\r\n", $data));

// Displaying all items above $10
$items = file($cowboyfile);
$item_filtred = array();
for ($i = 0; $i < count($items); $i++)
{
    $item = $items[$i];
    $item_price = substr($items[$i], strpos($item, ',') + 1);
    if ($item_price >= 10)
    {
        $item_filtred[] = $item;
    }
}
print_r($item_filtred);