0

Is there a faster way to add all the numbers in each segment of the multidimensional array rather than just doing it all manually? I was told before that a foreach loop could be used but I've hit a brick wall.

I'm trying to make it so it shows the total number of course enrollments in each campus and then the total number of students taking each course.

I feel like the answer is staring me in the face but I'm unsure.

<?

$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)
{

}
4
  • Is array_sum what you're looking for? Commented Oct 6, 2014 at 15:51
  • foreach($campus as $key => $values) { echo $key, ' -> ', array_sum($values); } Commented Oct 6, 2014 at 15:52
  • For the total number of students taking each course, you can use array_column() with array_sum() (if you're using PHP >= 5.5) otherwise use an array_map() instead of array_column() Commented Oct 6, 2014 at 15:53
  • I do not, how would one use it for this? I took a look at it and I can kinda see what it's doing. Commented Oct 6, 2014 at 15:53

5 Answers 5

1

Use array_sum() to add the numbers in an array, and use array_map() to apply it to each element of the $campus array.

$total_by_campus = array_map('array_sum', $campus);
Sign up to request clarification or add additional context in comments.

2 Comments

When I tried to do this, I ended up getting this error - Notice: Array to string conversion in C:\xampp\htdocs\Work\arrays_courses.php on line 43
What's on line 43? You can't echo an array, you have to use var_dump() to see what's in it.
0
$courses = array();
foreach($campus as $key=>$value)
{
    foreach($value as $course=>$num)
        $courses[$course] += $num;
}
var_dump($courses);

Should do it.

Comments

0

You can use PHP's array_sum() function that will add up the values of the array you give it.

<?php

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

Comments

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;

Comments

0

take a look:

$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, ...)
    }
}

Sorry, but you post a question: "How to check an array's contents to see if they have any numbers?"

<?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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.