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

时间:2018-10-25 08:28:12

标签: php laravel

我试图遍历一个简单的数组数组,然后,如果我在子数组中发现'id'值的重复项,请将它们分组并遍历它们。

这对您来说显而易见,但是我找不到简单的方法来做到这一点。

示例:

$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'
    )
);

所需结果:

所有ID为2135的条目都必须设置为蓝色。
ID 5342的所有条目都必须设置为黑色。

2 个答案:

答案 0 :(得分:-1)

    Import-Csv '<PATH_TO_THE_CSV_FILE>' -Header 'Server','Database'

输出

$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',
        'end' => 'august',
        'color' => 'green'
    ),
    array(
        'id' => 5342,
        'start' => 'april',
        'end' => 'june',
        'color' => 'purple'
    )
);


for ($i=0 ; $i<sizeof($records) ; $i++) {
    $test_id = $records[$i]['id'];
    for ($j=$i+1 ; $j<sizeof($records) ; $j++) {
        if ($records[$j]['id'] == $test_id) {
            $records[$j]['color'] = $records[$i]['color'];
            foreach ($records[$i] as $key => $value) {
                echo $key . ' -> ' . $value;
            }
            echo '<br /> --- ';
            foreach ($records[$j] as $key => $value) {
                echo $key . ' -> ' . $value;
            }
            echo '<br /> --- <br />';
        }
    }
}

答案 1 :(得分:-1)

尝试此解决方案

    $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);

输出

    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"
        ]
      ]
    ]

希望获得帮助