同一键下的PHP值合并

时间:2019-01-17 19:10:17

标签: php arrays

我有一个数组,我试图合并所有具有相同键名的子值,如果只有2个具有相同键名,则添加一个额外的值。

她是该数组具有92行的数组的saml示例

Array
(
    [0] => Array ( 
            [N_127y4Gral] => Array ( 
                    [0] => 302
                )
        )
    [1] => Array ( 
            [N_127y4Gral] => Array ( 
                    [0] => 288
                )
        )
    [2] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 317
                )
        )
    [3] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 307
                )
        )
    [4] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 332
                )
        )
)

这是我想要的输出。

Array
(
    [0] => Array ( 
            [N_127y4Gral] => Array ( 
                    [0] => 302
                    [1] => 288
                    [2] => 0
                )
        )
    [1] => Array ( 
            [N_144y4Gral] => Array ( 
                    [0] => 317
                    [1] => 307
                    [2] => 332
                )
        )
)

我认为我需要使用foreach或for循环来实现此功能,但是我不确定如何构造函数,因此非常感谢任何建议。

4 个答案:

答案 0 :(得分:0)

$result = [];
foreach ($data as $row) {
    foreach ($row as $key => $val) {
        $result[$key][] = $val[0];
    }
}

print_r($result);

答案 1 :(得分:0)

您可以使用array_walk对每个项目进行迭代,然后将值提取/复制到另一个数组中,因为您将在键名下使用单个值,也想添加一个{{1} }对于值数量小于其他子索引的那些索引,可以为此目的使用0,请参见下文

array_pad

上面将输出以下数组

$arr = [
    [
        'N_127y4Gral' => [302]
    ],
    [
        'N_127y4Gral' => [288]
    ],
    [
        'N_144y4Gral' => [317]
    ],
    [
        'N_144y4Gral' => [307]
    ],
    [
        'N_144y4Gral' => [332]
    ]
];

$newArr = [];

//walk array and get the values into another 
//array according to the desired format
array_walk(
    $arr,
    function ($val, $index) use (&$newArr) {
        $newArr[key($val)][] = $val[key($val)][0];
    }
);

//get the array key names
$keys = array_keys($newArr);

//iterate all key names 
foreach ($keys as $key => $keyName) {

    //check the array size if smaller than 3
    if (sizeof($newArr[$keyName]) < 3) {

        //add 0 for the missing indexes
        $newArr[$keyName] = array_pad($newArr[$keyName], 3, 0);
    }
}

print_r($newArr);

答案 2 :(得分:0)

如果循环数组并构建和关联数组,并保留每个数组的最大值,则可以使用引用将其循环的结果来修改结果数组。

foreach($arr as $sub){
    foreach($sub as $key => $val){
        $res[$key][] = $val[0];
        if(!isset($count[$key])){
            $count[$key] = 1;
        }else{
            $count[$key]++;
        }
    }
}
$max = max($count);
foreach($res as &$subarray){
    $subarray = array_merge($subarray, array_fill(0,$max-count($subarray), 0));
}
var_dump($res);

输出:

array(2) {
  ["N_127y4Gral"]=>
  array(3) {
    [0]=>
    string(3) "302"
    [1]=>
    string(3) "288"
    [2]=>
    int(0)
  }
  ["N_144y4Gral"]=>
  &array(3) {
    [0]=>
    string(3) "317"
    [1]=>
    string(3) "307"
    [2]=>
    string(3) "332"
  }
}

https://3v4l.org/WHWYg

答案 3 :(得分:0)

检查此循环:https://rextester.com/GOGA85799

 foreach($old_array as $key=>$value){
       $temp_key="";

       foreach($value as $okey=>$ovalue){
           echo $okey;
           echo $temp_key;
           if($okey!=$temp_key){
               echo "changed";
               $new_array[$okey][]=$ovalue[0];

           }
            $temp_key=$okey;

       }
   }