1

I have the following multidimensional array:

Array
(
    [0] => Array
        (
            [name] => Viettel
            [data] => Array
                (
                    [0] => 1559881644000,500
                )

        )

    [1] => Array
        (
            [name] => FPT
            [data] => Array
                (
                    [0] => 1559994465000,172
                )

        )

    [2] => Array
        (
            [name] => MobiFone
            [data] => Array
                (
                    [0] => 1559997314000,11164
                )

        )

    [3] => Array
        (
            [name] => Vietnamobile
            [data] => Array
                (
                    [0] => 1559993699000,1246
                )

        )

    [4] => Array
        (
            [name] => Viettel
            [data] => Array
                (
                    [0] => 1560000096000,47886
                )

        )

    [5] => Array
        (
            [name] => VinaPhone
            [data] => Array
                (
                    [0] => 1559997553000,11132
                )

        )

    [6] => Array
        (
            [name] => VNPT
            [data] => Array
                (
                    [0] => 1559993066000,84
                )

        )

)

I'm currently using a foreach loop to extract the values from the array:

      $result = [];
        $nameData = [];
        foreach($data as $key => $itemData)
        {

        }

I want array look like this:

Array
(
    [0] => Array
        (
            [name] => Viettel
            [data] => Array
                (
                    [0] => 1559881644000,500
                    [1] => 1560000096000,47886
                )

        )

    [1] => Array
        (
            [name] => FPT
            [data] => Array
                (
                    [0] => 1559994465000,172
                )

        )

    [2] => Array
        (
            [name] => MobiFone
            [data] => Array
                (
                    [0] => 1559997314000,11164
                )

        )

    [3] => Array
        (
            [name] => Vietnamobile
            [data] => Array
                (
                    [0] => 1559993699000,1246
                )

        )

    [4] => Array
        (
            [name] => VinaPhone
            [data] => Array
                (
                    [0] => 1559997553000,11132
                )

        )

    [5] => Array
        (
            [name] => VNPT
            [data] => Array
                (
                    [0] => 1559993066000,84
                )

        )

)

3 Answers 3

1

It seems you are looking for groupby the array by name.

You can use foreach with array_key_exists and array_push

 $groupBy = [];
 foreach($a as $v){
   array_key_exists($v['name'], $groupBy) ? 
    array_push($groupBy[$v['name']]['data'], $v['data'][0])
   :
    ($groupBy[$v['name']] = $v)
   ;
 }

If you want to rearrange the keys of array, you can use array_values

 print_r(array_values($groupBy));

Working DEMO :- https://3v4l.org/ASWDV

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

Comments

0

You can just loop over the input array, adding values to the output if they don't exist and merging that data values if they do:

$result = array();
foreach ($data as $itemData) {
    if (($k = array_search($itemData['name'], array_column($result, 'name'))) !== false) {
        $result[$k]['data'] = array_merge($result[$k]['data'], $itemData['data']);
    }
    else $result[] = $itemData;
}
print_r($result);

Output is as you desire (too long to reproduce here)

Demo on 3v4l.org

Comments

0

If you are adding a single value to the array you can just access to it with something like this:

$result[0]['data'][1] = 1560000096000,47886

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.