0

how to display this using foreach in php? I want to show it as a list.

$quantity=0;
        $quantities = array(
            array('Jan',($quantity += $ppmpitem->m1)),
            array('Feb',($quantity += $ppmpitem->m2)),
            array('Mar',($quantity += $ppmpitem->m3)),
            array('Apr',($quantity += $ppmpitem->m4)),
            array('May',($quantity += $ppmpitem->m5)),
            array('Jun',($quantity += $ppmpitem->m6)),
            array('Jul',($quantity += $ppmpitem->m7)),
            array('Aug',($quantity += $ppmpitem->m8)),
            array('Sep',($quantity += $ppmpitem->m9)),
            array('Oct',($quantity += $ppmpitem->m10)),
            array('Nov',($quantity += $ppmpitem->m11)),
            array('Dec',($quantity += $ppmpitem->m12)),
        );
        return $quantities;
1
  • 2
    Are you looking for something automatic like print_r? var_dump? If you want it formatted a certain way, use a for each like you said in your question. Iterate through it and echo out whatever HTML you want. Commented Jan 11, 2022 at 8:17

3 Answers 3

1

You have several options, so based on your code (and what I added):

<?php
$quantity=0;

// Added this, so we have an object
$array = [
        "m1" => 1,
        "m2" => 2,
        "m3" => 3,
        "m4" => 4,
        "m5" => 5,
        "m6" => 6,
        "m7" => 7,
        "m8" => 8,
        "m9" => 9,
        "m10" => 10,
        "m11" => 11,
        "m12" => 12
    ];
// Converted array to object    
$ppmpitem = json_decode(json_encode($array));

$quantities = array(
    array('Jan',($quantity += $ppmpitem->m1)),
    array('Feb',($quantity += $ppmpitem->m2)),
    array('Mar',($quantity += $ppmpitem->m3)),
    array('Apr',($quantity += $ppmpitem->m4)),
    array('May',($quantity += $ppmpitem->m5)),
    array('Jun',($quantity += $ppmpitem->m6)),
    array('Jul',($quantity += $ppmpitem->m7)),
    array('Aug',($quantity += $ppmpitem->m8)),
    array('Sep',($quantity += $ppmpitem->m9)),
    array('Oct',($quantity += $ppmpitem->m10)),
    array('Nov',($quantity += $ppmpitem->m11)),
    array('Dec',($quantity += $ppmpitem->m12)),
);
// Pretty print
print("<pre>".print_r($quantities,true)."</pre>");
// As a list where Month is key
foreach($quantities as $key => $value) {
    $month = [
            $quantities[$key][0] => $quantities[$key][1]
        ];
    $months[$quantities[$key][0]]  = $quantities[$key][1];
    print_r($month);   
}

//Also print full array of months:
print_r($months);

Results are: Pretty print:

<pre>Array
(
    [0] => Array
        (
            [0] => Jan
            [1] => 1
        )

    [1] => Array
        (
            [0] => Feb
            [1] => 3
        )

    [2] => Array
        (
            [0] => Mar
            [1] => 6
        )

    [3] => Array
        (
            [0] => Apr
            [1] => 10
        )

    [4] => Array
        (
            [0] => May
            [1] => 15
        )

    [5] => Array
        (
            [0] => Jun
            [1] => 21
        )

    [6] => Array
        (
            [0] => Jul
            [1] => 28
        )

    [7] => Array
        (
            [0] => Aug
            [1] => 36
        )

    [8] => Array
        (
            [0] => Sep
            [1] => 45
        )

    [9] => Array
        (
            [0] => Oct
            [1] => 55
        )

    [10] => Array
        (
            [0] => Nov
            [1] => 66
        )

    [11] => Array
        (
            [0] => Dec
            [1] => 78
        )

)
</pre>

Print out "list":

Array
(
    [Feb] => 3
)
Array
(
    [Mar] => 6
)
Array
(
    [Apr] => 10
)
Array
(
    [May] => 15
)
Array
(
    [Jun] => 21
)
Array
(
    [Jul] => 28
)
Array
(
    [Aug] => 36
)
Array
(
    [Sep] => 45
)
Array
(
    [Oct] => 55
)
Array
(
    [Nov] => 66
)
Array
(
    [Dec] => 78
)

Or print one array (month as key)

Array
(
    [Jan] => 1
    [Feb] => 3
    [Mar] => 6
    [Apr] => 10
    [May] => 15
    [Jun] => 21
    [Jul] => 28
    [Aug] => 36
    [Sep] => 45
    [Oct] => 55
    [Nov] => 66
    [Dec] => 78
) 

BR

Sign up to request clarification or add additional context in comments.

Comments

1

You may create an array filled from 1 to 12 and apply the array_reduce to get your output.

<?php

class P {
    public $m1 = 1;
    public $m2 = 1;
    public $m3 = 1;
    public $m4 = 1;
    public $m5 = 1;
    public $m6 = 1;
    public $m7 = 1;
    public $m8 = 1;
    public $m9 = 1;
    public $m10 = 1;
    public $m11 = 1;
    public $m12 = 1;

    public function __construct() {
    }
}

$ppmpitem = new P();

$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

$quantities = array_reduce($array, function ($carry, $item) use ($ppmpitem) {
    $month = DateTime::createFromFormat('!m', $item)->format('M');
    $carry['total'] +=  $ppmpitem->{"m" . $item};
    $carry['quantities'][] = [$month, $carry['total']];
    return $carry;
}, ['total' => 0, 'quantities' => []])['quantities'];

var_dump($quantities);

Comments

-1

There are a number of ways to display a multidimensional array including use of the default PHP's print_r() function. Besides if you want a more precise way of accessing the elements and displaying them you can use a looping criteria such as for() or foreach(). Sample :

foreach($quantities as $qty){
    print_r($qty);
}

2 Comments

How is this printing out array? This prints string..
had initially demonstrated on how to print the array values .. print_r() can be used to print the arrays.

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.