简体   繁体   English

循环遍历子数组中的键值重复项

[英]Loop through key value duplicates in sub array

I'm trying to iterate through a simple array of arrays, then if I find duplicates of 'id' value in a sub arrays, group them and iterate through them.我试图遍历一个简单的数组数组,然后如果我在子数组中找到重复的'id'值,将它们分组并遍历它们。

This can be obvious for you but I can't find a simple way to do this.这对你来说很明显,但我找不到一种简单的方法来做到这一点。

Example:例子:

$records = array(
    array(
        'id' => 2135,
        'start' => 'january',
        'end' => 'march',
        'color' => 'blue'
    ),
    array(
        'id' => 2135,
        'start' => 'march',
        'end' => 'april',
        'color' => 'red'
    ),
    array(
        'id' => 5342,
        'start' => 'january',
        'end' => 'july',
        'color' => 'black'
    ),
    array(
        'id' => 5623,
        'start' => 'march',
        'key' => 'august',
        'color' => 'green'
    ),
    array(
        'id' => 5342,
        'start' => 'april',
        'end' => 'june',
        'color' => 'purple'
    )
);

Result needed:需要的结果:

All entries of id 2135 must be set to color blue. id 2135 的所有条目必须设置为蓝色。
All entries of id 5342 must be set to color black. id 5342 的所有条目必须设置为黑色。

Try this solution试试这个解决方案

    $records = array(
        array(
            'id' => 2135,
            'start' => 'january',
            'end' => 'march',
            'color' => 'blue'
        ),
        array(
            'id' => 2135,
            'start' => 'march',
            'end' => 'april',
            'color' => 'red'
        ),
        array(
            'id' => 5342,
            'start' => 'january',
            'end' => 'july',
            'color' => 'black'
        ),
        array(
            'id' => 5623,
            'start' => 'march',
            'key' => 'august',
            'color' => 'green'
        ),
        array(
            'id' => 5342,
            'start' => 'april',
            'end' => 'june',
            'color' => 'purple'
        )
    );
    $collection = collect($records);
    $grouped = $collection->groupBy('id')->map(function($entries, $key){
        $color = collect($entries)->first()['color'];
        $e = $entries->toArray();
        foreach ($e as $key => $value) {
            $e[$key]['color'] = $color;
        }
        return $e;
    })->values()->all();
    dd($grouped);

Outputs输出

    array:3 [▼
      0 => array:2 [▼
        0 => array:4 [▼
          "id" => 2135
          "start" => "january"
          "end" => "march"
          "color" => "blue"
        ]
        1 => array:4 [▼
          "id" => 2135
          "start" => "march"
          "end" => "april"
          "color" => "blue"
        ]
      ]
      1 => array:2 [▼
        0 => array:4 [▼
          "id" => 5342
          "start" => "january"
          "end" => "july"
          "color" => "black"
        ]
        1 => array:4 [▼
          "id" => 5342
          "start" => "april"
          "end" => "june"
          "color" => "black"
        ]
      ]
      2 => array:1 [▼
        0 => array:4 [▼
          "id" => 5623
          "start" => "march"
          "key" => "august"
          "color" => "green"
        ]
      ]
    ]

Hope this help希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM