Merging arrays based on a value of the key

前端 未结 9 1317
予麋鹿
予麋鹿 2020-12-19 09:03

I have two arrays of arrays that have an id key, and I\'d like to merge the data together based on that array\'s key and key value. The data would look somethin

9条回答
  •  再見小時候
    2020-12-19 09:43

    I'd suggest using laravel's collections, since this question has the laravel tag.

    $color = collect(
        ['id' => 1, 'color' => 'red'],
        ['id' => 2, 'color' => 'green'],
        ['id' => 3, 'color' => 'blue']
    );
    
    $size = collect(
        ['id' => 1, 'size' => 'SM'],
        ['id' => 2, 'size' => 'XL'],
        ['id' => 3, 'size' => 'MD'],
        ['id' => 4, 'size' => 'LG']
    );
    
    $combined = $color->merge($size);
    

提交回复
热议问题