重新排列数组数据并在新数组 php 中分开

数组a


Array

(

    [0] => Array

        (

            [member_id] => 6

            [sorting] => 0

            [total] => 1

        )


    [1] => Array

        (

            [member_id] => 7

            [sorting] => 1

            [total] => 2

        )


)

数组b


Array

(

    [0] => Array

        (

            [member_id] => 2

            [total] => 3

        )


    [1] => Array

        (

            [member_id] => 6

            [total] => 1

        )


    [2] => Array

        (

            [member_id] => 7

            [total] => 2

        )


)

问题:以上数据我尝试循环数组并存储到新数组中,如下所示(最终数据)。首先,如果第二个数组缺少成员数据,那么我需要从第一个数组中获取成员数据并将其存储到新数组中。除此之外,如果缺少排序,我需要从第一个数组中获取排序值。最后,我能够很好地获取所有数据,但最后存储到其中的值将会重复。任何人都可以帮忙:(?我的逻辑停留在这里。


我的代码返回


Array

(

    [0] => Array

        (

            [member_id] => 6

            [total] => 1

            [sorting] => 0

        )


    [1] => Array

        (

            [member_id] => 7

            [total] => 2

            [sorting] => 1

        )


    [2] => Array

        (

            [member_id] => 7

            [total] => 2

            [sorting] => 

        )


)

最终数据应该是:


Array

(

    [0] => Array

        (

            [member_id] => 6

            [total] => 1

            [sorting] => 0

        )


    [1] => Array

        (

            [member_id] => 7

            [total] => 2

            [sorting] => 1

        )


    [2] => Array

        (

            [member_id] => 2

            [total] => 3

            [sorting] => 2

        )


)

代码: https: //3v4l.org/WBLCC


慕田峪4524236
浏览 166回答 2
2回答

慕娘9325324

// create a version of $a that uses the member_id as key, makes look-up easier$a_reindexed = array_combine( array_column($a, 'member_id'), array_values($a) );// get the current maximum sorting value from the elements in $a$max_sort = max(array_column($a, 'sorting'));$result = [];// add values from $b to the resultforeach($b as $key => $value) {&nbsp; &nbsp; $result[$key] = $value;&nbsp; &nbsp; // set the sorting property to that of the corresponding element from $a_reindexed&nbsp; &nbsp; // or PHP_INT_MAX if the former doesn’t exist&nbsp; &nbsp; $result[$key]['sorting'] = $a_reindexed[$value['member_id']]['sorting'] ?? PHP_INT_MAX;}// sort the result array by the sorting value, so all elements with PHP_INT_MAX will go to the endusort($result, function($a, $b) { return $a['sorting'] <=> $b['sorting']; });// set new sorting value for all items having it set to PHP_INT_MAXforeach($result as $key => $value) {&nbsp; if($value['sorting'] == PHP_INT_MAX) {&nbsp; &nbsp; $result[$key]['sorting'] = ++$max_sort;&nbsp; }}

梦里花落0921

可能适合您需求的基本解决方案:/*&nbsp;* first, store all records with matching member_id into store $c&nbsp;*/$c = [];foreach ($b as $key0 => $value0) {&nbsp; &nbsp; foreach ($a as $key1 => $value1) {&nbsp; &nbsp; &nbsp; &nbsp; // matching 'member_id'?&nbsp; &nbsp; &nbsp; &nbsp; if ($value0['member_id'] === $value1['member_id']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // inject record into store $c&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $c[] = $a[$key1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}/*&nbsp;* second, inject non-matching records from $b into store $c&nbsp;*/$ids = array_column($c, 'member_id');foreach ($b as $key => $value) {&nbsp; &nbsp; if (!in_array($value['member_id'], $ids)) {&nbsp; &nbsp; &nbsp; &nbsp; $sorting = max(array_column($c, 'sorting')) + 1;&nbsp; &nbsp; &nbsp; &nbsp; $value['sorting'] = $sorting;&nbsp; &nbsp; &nbsp; &nbsp; $c[] = $value;&nbsp; &nbsp; }}工作演示
打开App,查看更多内容
随时随地看视频慕课网APP