0

I need to group array by date but not working. unable to push object into sub array. below are my tried code.

$getData = array
(
    0 => array
    (
        'date' => '2017-08-22',
        'msg' => '1231',
    ),
    1 => array
    (
        'date' => '2017-08-21',
        'msg' => '1172',
    ),
    2 => array
    (
        'date' => '2017-08-20',
        'msg' => '1125'
    ),
    3 => array
    (
        'date' => '2017-08-21',
        'msg' => '251'
    ),
    4 => array
    (
        'date' => '2017-08-20',
        'msg' => '21773',
    ),
    5 => array
    (
        'date' => '2017-08-22',
        'msg' => '3750'
    )
);
$smsArr = [];
foreach ($getData as $xyz => $groupData) {
    $isFound = FALSE;
    $abc['date'] = $groupData['date'];
    $abc['data'] = [(object)["msg" => $groupData['msg']]];
    foreach ($smsArr as $key => $value) {
        if ($value['date'] == $groupData['date']) {
            array_push($value['data'], (object)["msg" => $groupData['msg']]); // nothing happens to final array
            $isFound = TRUE;
        }
    }
    if ($isFound == FALSE) {
        array_push($smsArr, $abc);
    }


}
print_r($smsArr);

trying to group json by date. but when trying to push element to sub array nothing happens.

i should get final result

Array
(
    [0] => Array
    (
        [date] => 2017 - 08 - 21
        [data] => Array
        (
            [0] => stdClass Object
            (
                [msg] => 1172
            )
            [1] => stdClass Object
            (
                [msg] => 251
            )
        )
    )

    [1] => Array
    (
        [date] => 2017 - 08 - 20
        [data] => Array
        (
            [0] => stdClass Object
            (
                [msg] => 1125
            )
            [1] => stdClass Object
            (
                [msg] => 21773
            )
        )
    )
    
    [2] => Array
    (
        [date] => 2017 - 08 - 21
        [data] => Array
        (
            [0] => stdClass Object
            (
                [msg] => 3750
            )
            [1] => stdClass Object
            (
                [msg] => 1231
            )
        )
    )
)

when I trying push array_push($value['data'],(object)["msg"=>$groupData['msg']]);

it does not reflect on final array.

1
  • That’s because foreach works in a copy of the array. You either need to write to $getData using the appropriate keys, or work with references. Commented Jan 11, 2021 at 9:53

1 Answer 1

1

When you want to change the array in foreach you should pass its value by reference. so the line:

foreach ($smsArr as $key => $value)

change it to

foreach ($smsArr as $key => &$value)

now when you change the $value, $smsArr will change. this document will help you to know more about the concept. https://www.php.net/manual/en/language.references.pass.php

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

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.