search

Home  >  Q&A  >  body text

javascript - php array merging problem

Array
(

[村西] => Array
    (
        [0] => Array
            (
                [headimg] =>'头像'
                [info] => 村西                
                [product_name] =>产品1
            )

        [1] => Array
            (   
                [headimg] =>'头像'        
                [info] => 村西             
                [product_name] => 产品2
            )

    )

[村东] => Array
    (
        [0] => Array
            (   
                [headimg] =>'头像'               
                [info] => 村东
                [product_name] => 产品3
            )

        [1] => Array
            (   
                [headimg] =>'头像'                
                [info] => 村东
                [product_name] => 产品4
            )
        [2] => Array
            (   
                [headimg] =>'头像'                
                [info] => 村东
                [product_name] => 产品5
            )

    )

[小邢庄村口] => Array
    (
        [0] => Array
            (   
                [headimg] =>'头像'             
                [info] => 小邢庄村口                  
                [product_name] => 产品6
            )

    )

Now the info has been merged into an array, that is, 'Village East' and 'Village West'. . Now I want to merge product_names in the same info together.
For example, 'Product 1' and 'Product 2' in Cunxi are merged together in the form of an array.

In addition, the health value of headimg should be retained
Thank you very much

我想大声告诉你我想大声告诉你2791 days ago650

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 13:16:34

        $data = array();
        foreach ($a as $k=>$v) {
    
            foreach($v as $key=>$val){
                if ($val['name'] == $k){
                    $data[$k][$key]['product_name'] = $val['product_name'];
                    $data[$k][$key]['head'] = $val['head'];
                }
            }
        }

    reply
    0
  • PHPz

    PHPz2017-05-16 13:16:34

    <?php
    $result=array();
    foreach($arr as $k=>$v){
        foreach($v as $item){
            $result[$k][]=$item['product_name'];
        }
    }

    reply
    0
  • Cancelreply