dow56114 2018-04-25 20:38
浏览 44
已采纳

按公共值对多维数组进行排序和合并

I have a multidimensional array and I'm trying to sort it by common value of one particular key, name in my case.

I already tried using a foreach to loop into the array, or using usort, but I just can't find the way to do it.

Notice how the values of the "Diamètre" key are merged into an array in the final result.

Array
(
    [0] => Array
        (
            [0]  => Array
                (
                    [name]  => Diamètre
                    [value]  => 5                   
                )

            [1]  => Array
                (
                    [name]  => Force
                    [value]  => 30                  
                )

        )

    [1]  => Array
         (

            [0]  => Array
                (
                    [name]  => Force
                    [value]  => 20             
                )

            [1]  => Array
                (
                    [name]  => Poids
                    [value]  => 50         
                )
          )
     [2]  => Array
         (
            [0]  => Array
                (
                    [name]  => Diamètre
                    [value]  => 40
                )

            [1]  => Array
                (
                    [name]  => Largeur
                    [value]  => 40
                )

          )
)

Expected result :

Array
(

            [0]  => Array
                (
                    [name]  => Diamètre
                    [value]  => Array (5, 40)                                                              
                )

            [1]  => Array
                (
                    [name]  => Force
                    [value]  => Array (30, 20)               
                )

             [2]  => Array
                 (
                    [name]  => Poids
                    [value]  => 50        
                 ) 

             [3]  => Array
               (
                    [name]  => Largeur
                    [value]  => 40
                )     
)
  • 写回答

1条回答 默认 最新

  • dongying9756 2018-04-25 20:55
    关注

    You could try something along the lines of:

    $result = [];
    
    foreach($array1 as $array2) {
        foreach($array2 as $item) {
    
            if(array_key_exists($item['name'], $result)) {
                $newItem = $result[$item['name']];
    
                $valueArray = (array) $item['value'];
                $newValueArray = (array) $newItem['value'];
    
                $newItem['value'] = array_merge($valueArray, $newValueArray);
            } else {
                $newItem = $item;
            }
    
                $result[$item['name']] = $newItem;
        }
    }
    
    // In case you don't want an associative array.
    $result = array_values($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?