Skip to content
Advertisement

Rearrage the data into new array in php

First

Array
(
    [0] => Array
        (
            [member_id] => 1
            [total] => 0
            [sorting] => 0
        )

    [1] => Array
        (
            [member_id] => 2
            [total] => 0
            [sorting] => 1
        )

)

Second

Array
(   
    [0] => Array
        (
            [member_id] => 1
            [total] => 2
        )
    [1] => Array
        (
            [member_id] => 3
            [total] => 4
        )
)

Sorry for asking the stupid question, I get stuck in this issue because I very weak in the logic thinking.

Question: How can I rearrange the data into a new array like the below (Final data). First of all, if the second array is missing the member data, then I need to get from the first array and store it into a new array. Besides that, I need to get the sorting value from the first array if the sorting is missing.

final data

Array
(   
    [0] => Array
        (
            [member_id] => 1
            [total] => 2
            [sorting] => 0
        )
    [1] => Array
        (
            [member_id] => 2
            [total] => 0
            [sorting] => 1
        )
    [2] => Array
        (
            [member_id] => 3
            [total] => 4
            [sorting] => 2
         )
)

Advertisement

Answer

You can achieve this by the following code :

  $a = Array
        (
        0 => Array
            (
            'member_id' => 1,
            'total' => 0,
            'sorting' => 0
        ),
        1 => Array
            (
            'member_id' => 2,
            'total' => 0,
            'sorting' => 1
        )
    ); 

 $b = Array
        (
        0 => Array
            (
            'member_id' => 1,
            'total' => 2
        ),
        1 => Array
            (
            'member_id' => 3,
            'total' => 4
        )
    );


   $new_array = [];
    foreach ($a as $key => $data) {
        $new_array[$key]['member_id'] = $data['member_id'];
        foreach ($b as $key_1 => $data_1) {
            if ($data['member_id'] == $data_1['member_id']) {
                $new_array[$key]['total'] = $data_1['total'];
                if (isset($data_1['sorting'])) {
                    $new_array[$key]['sorting'] = $data_1['sorting'];
                } else {
                    $new_array[$key]['sorting'] = $data['sorting'];
                }
                break;
            } else {
                $new_array[$key]['total'] = $data['total'];
                $new_array[$key]['sorting'] = $data['sorting'];
            }
        }
    }
    
    $new_array1 = [];
    foreach ($b as $key => $data) {
        if ($data['member_id'] != $new_array[$key]['member_id']) {
            $new_array1[$key]['member_id'] = $data['member_id'];
            $new_array1[$key]['total'] = $data['total'];
            if (!isset($data['sorting'])) {
                $new_array1[$key]['sorting'] = $new_array[$key]['sorting'] + 1;
            }
        }
    }

    $array_merge = array_merge($new_array, $new_array1);

Final Result:

  array:3 [▼
        0 => array:3 [▼
          "member_id" => 1
          "total" => 2
          "sorting" => 0
        ]
        1 => array:3 [▼
          "member_id" => 2
          "total" => 0
          "sorting" => 1
        ]
        2 => array:3 [▼
          "member_id" => 3
          "total" => 4
          "sorting" => 2
        ]
      ]
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement