Home » Php » Creating Array inside another array using foreach loop php

Creating Array inside another array using foreach loop php

Posted by: admin February 25, 2020 Leave a comment

Questions:

Its been four hours I have search everywhere on google and SOF but unable to find the answer. This is what i have tried so far. Here is my code so far

        $tmp_array = array();
        foreach ($cms as $key => $val) {

            $cDate = date('Ym', strtotime($val['day_date']));
            $tmp_ids[] = $val['id'];

            if (array_key_exists($cDate, $tmp_array)) {
                $tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
                $tmp_array[$cDate]['ids'] = $tmp_ids;
            } else {
                $tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
                $tmp_array[$cDate]['ids'] = $tmp_ids;
            }

        }

its output is coming like this,

Array
(
    [202001] => Array
        (
            [new_visitors] => 797
            [ids] => Array
                (
                    [0] => 31
                    [1] => 32
                )

        )

    [202002] => Array
        (
            [new_visitors] => 461
            [ids] => Array
                (
                    [0] => 31
                    [1] => 32
                    [2] => 33
                    [3] => 34
                )

        )

)

but i want the result array like this,

Array
(
    [202001] => Array
        (
            [new_visitors] => 797
            [ids] => Array
                (
                    [0] => 31
                    [1] => 32
                )

        )

    [202002] => Array
        (
            [new_visitors] => 461
            [ids] => Array
                (
                    [0] => 33
                    [1] => 34
                )

        )

)

Any suggestions, what am i doing in my code?

The ids are basicall primary key of the table and the “new_visitors” are count of the visitor those who visit on my site. but i havent find any solution so far.

Here is my $cms array.

Array
(
    [0] => Array
        (
            [id] => 31
            [day_date] => 2020-01-30 00:00:00
            [new_visitors] => 459
        )

    [1] => Array
        (
            [id] => 32
            [day_date] => 2020-01-31 00:00:00
            [new_visitors] => 338
        )

    [2] => Array
        (
            [id] => 33
            [day_date] => 2020-02-01 00:00:00
            [new_visitors] => 242
        )

    [3] => Array
        (
            [id] => 34
            [day_date] => 2020-02-02 00:00:00
            [new_visitors] => 219
        )

)
How to&Answers:

Thank you misorude, your comment worked

  $tmp_array = array();
                foreach ($cms as $key => $val) {
                    $cDate = date('Ym', strtotime($val['day_date']));
                    if (array_key_exists($cDate, $tmp_array)) {
                        $tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
                        $tmp_array[$cDate]['ids'][] = $val['id'];
                    } else {
                        $tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
                        $tmp_array[$cDate]['ids'][] = $val['id'];
                    }
                }

Answer:

1.Instead of assigning all id’s like $tmp_ids[] = $val['id']; Assign $val['id'] directly.

2.Use array_keys() inside array_key_exists() check.

Do like below:

$tmp_array = array();
foreach ($cms as $key => $val) {

    $cDate = date('Ym', strtotime($val['day_date']));

    if (array_key_exists($cDate, array_keys($tmp_array))) {
        $tmp_array[$cDate]['new_visitors'] += $val['new_visitors'];
    } else {
        $tmp_array[$cDate]['new_visitors'] = $val['new_visitors'];
    }
    $tmp_array[$cDate]['ids'][] = $val['id'];

}

More simplified version using ternary operator:

$tmp_array = array();
foreach ($cms as $key => $val) {

    $cDate = date('Ym', strtotime($val['day_date']));

    $tmp_array[$cDate]['new_visitors'] = (isset($tmp_array[$cDate]['new_visitors'])) ? $tmp_array[$cDate]['new_visitors'] + $val['new_visitors'] : $val['new_visitors'];
    $tmp_array[$cDate]['ids'][] = $val['id'];

}

Output:- https://3v4l.org/op6mq OR https://3v4l.org/5R8AQ

Answer:

$arr = [
    ['id' => 31 ,'day_date'=> "2020-01-30 00:00:00", 'new_visitors' => 459],
    ['id' => 32 ,'day_date'=> "2020-01-31 00:00:00", 'new_visitors' => 338],
    ['id' => 33 ,'day_date'=> "2020-02-01 00:00:00", 'new_visitors' => 242],
    ['id' => 34 ,'day_date'=> "2020-02-02 00:00:00", 'new_visitors' => 219]
];
echo "<pre>";
print_r($arr);
$data = [];
$add_visitor = 0;
$day_date = '';
foreach($arr as $arr_value){

    if(date('Ym', strtotime($arr_value['day_date'])) != $day_date){
        $add_visitor = 0;
    }
    $add_visitor += $arr_value['new_visitors'];
    $data[date('Ym', strtotime($arr_value['day_date']))]['new_visitors'] = $add_visitor;
    $data[date('Ym', strtotime($arr_value['day_date']))]['ids'][] = $arr_value['id'];
    $day_date = date('Ym', strtotime($arr_value['day_date']));
}

print_r($data);