0

I have sn array of values, for each month of the last few months. Each month has the number of enquiries, quotes and orders. I am trying to create an array for each month, and then for said month it will hold the values for enquiries, quotes and orders, so I can then output all 3 values for each month.

My problem is that when I use foreach for $arr[$y][$m] as $a, the $a does not seem to contain the other array elements of enquiries, quotes or orders, i.e. $a['enquiries'] yields no value.

How can I obtain the values, or is there a much simpler way of doing this?

// creating the array

$arr = array();
$arr[19][03]['enquiries'] = 34;
$arr[19][02]['enquiries'] = 24;
$arr[19][01]['enquiries'] = 28;
$arr[18][12]['enquiries'] = 42;

$arr[19][03]['quotes'] = 22;
$arr[19][02]['quotes'] = 14;
$arr[19][01]['quotes'] = 11;
$arr[18][12]['quotes'] = 23;

$arr[19][03]['orders'] = 15;
$arr[19][02]['orders'] = 9;
$arr[19][01]['orders'] = 6;
$arr[18][12]['orders'] = 11;

// extrapolating the values

$y = 19;
$m = 03;
for ($x = 0; $x <= 3; $x++) {

// outputting the values

    foreach($arr[$y][$m] as $a) {
        echo $y.' '.$m.' enquiries='.$a['enquiries'].'<br>';
        echo $y.' '.$m.' quotes='.$a['quotes'].'<br>';
        echo $y.' '.$m.' orders='.$a['orders'].'<br>';
    }

    // creating the units the previous month

    $m--;
    if($m<1) {
        $m = 12;
        $y = $y--;
    }
}

Outputted array:

Array
(
    [19] => Array
        (
            [3] => Array
                (
                    [enquiries] => 34
                    [quotes] => 22
                    [orders] => 15
                )

            [2] => Array
                (
                    [enquiries] => 24
                    [quotes] => 14
                    [orders] => 9
                )

            [1] => Array
                (
                    [enquiries] => 28
                    [quotes] => 11
                    [orders] => 6
                )

        )

    [18] => Array
        (
            [12] => Array
                (
                    [enquiries] => 42
                    [quotes] => 23
                    [orders] => 11
                )

        )

)

1 Answer 1

2

You don't need a foreach. $arr[$y][$m] already points at the array with keys enquiries etc. so you can just access the values directly:

echo $y.' '.$m.' enquiries='.$arr[$y][$m]['enquiries'].'<br>';
echo $y.' '.$m.' quotes='.$arr[$y][$m]['quotes'].'<br>';
echo $y.' '.$m.' orders='.$arr[$y][$m]['orders'].'<br>';
Sign up to request clarification or add additional context in comments.

1 Comment

great much easier, dont know why I thought I needed a foreach in there

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.