如何根据键值合并数组值

时间:2015-03-17 09:52:32

标签: php arrays

我有这样的数组

SKU   Color                    Image
11      Black                     Image1
11      Black                     Image2
11      Black                     Image3
12      Red                     Image4
12      Red                     Image5

我想将图像合并为每个记录的一个值 像这样

SKU   Color                    Image
11      Black                 Image1 ,Image 2 , Image3
12      Red                 Image4 , Image 5

这里是var dump

array (size=3886)
      0 => 
        array (size=4)
          0 => string 'GROUP SKU' (length=9)
          1 => string 'COLOR' (length=5)
          2 => string 'SORT' (length=4)
          3 => string 'FILENAME' (length=8)
      1 => 
        array (size=4)
          0 => string '1007109' (length=7)
          1 => string 'Black' (length=5)
          2 => string '1' (length=1)
          3 => string '11.jpg' (length=6)
      2 => 

任何帮助

2 个答案:

答案 0 :(得分:1)

试试这个 -

  <?php 

    $my_Array = array(
        '0'=>array(
                'SKU'=>11,
                'color'=>'Black',
                'image'=>'Image1'
              ),
        '1'=>array(
               'SKU'=>11,
               'color'=>'Black',
               'image'=>'Image2'
             ),
        '2'=>array(
               'SKU'=>11,
               'color'=>'Black',
               'image'=>'Image3'
             ),
        '3'=>array(
              'SKU'=>12,
              'color'=>'Red',
              'image'=>'Image4'
            ),
        '4'=>array(
              'SKU'=>12,
              'color'=>'Red',
              'image'=>'Image5'
            ),
        '5'=>array(
              'SKU'=>13,
              'color'=>'Orange',
              'image'=>'Image6'
            ),    
        '6'=>array(
               'SKU'=>13,
               'color'=>'Orange',
               'image'=>'Image7'
            )
 );


            echo "<pre/>";print_r($my_Array);

            $newArray = array();


            foreach($my_Array as $key => $value){
                if($key == 0){
                    $newArray[] = $value;
                }else{
                    foreach ($newArray as $k => $val){
                        if($value['SKU'] == $val['SKU'] && $value['color'] == $val['color']){
                            $check = 1;
                            $newArray[$k]['image'] = implode(',', array($val['image'],$value['image']));
                            break;
                        }else{
                            $check =0;
                        }
                    }
                    if(trim($check) == 0){ 
                            $newArray[] = $value;
                        }
                }

            }
            echo "<pre/>";print_r($newArray);
            ?>

输出:http://prntscr.com/6i7kyi

答案 1 :(得分:0)

<?php
$result=array();    
foreach($array as $key=>$item)// 
{     
    if($key>0)// Since your first row is returning headers
    {
        if($item[1]=='black')//Color is at index 1
        {

             $result['black'][]=$item; 

        }
        else if($item[1]=='red')//Color is at index 1
        {
             $result['red'][]=$item; 
        } 
    } 


}

?>

所以$ result [&#39; black&#39;]包含黑色项目,$ result [&#39; red&#39;]包含红色项目