在php中合并多维数组中的相同键

时间:2014-10-08 18:28:21

标签: php

我有一个如下所示的数组我希望合并具有相同键的数组

Array
(
    [charts_group_name] => all hosts
    [hosts] => Array
    (
        [0] => Array
        (
            [Redmine] => CPU load
        )
        [1] => Array
        (
           [Redmine] =>CPU utilization
        )
        [2] => Array
        (
            [test123] => Kernel Process Creations
        )
        [3] => Array
        (
            [test123] => Memory used
        )
    )
)

但我想在下面,我的意思是我想合并具有相同名称的密钥,如redmine,test123 ..

Array
(
    [charts_group_name] => all hosts
    [hosts] => Array
    (
        [0] => Array
        (
            [Redmine] =>Array
            (
                [0] => CPU load
                [1] =>CPU utilization
            )
        )
        [1] => Array
        (
           [test123] => Array
           (
                [0] =>Memory used
                [1] =>Number of threads
           )
        )
    )
)

我试过这段代码..但我没有得到答案

foreach($multiplehostconfigarray as $item) {
    foreach($item as $it) { 
        $serv = key($item); 
        $host = current($item);
        if(!isset($result[$serv ])) { 
            $result[$artist] = array(); 
        } 
        print_r($result[$serv][] = $host );
    } `
}

3 个答案:

答案 0 :(得分:0)

$mArray = array(
    'charts_group_name' => 'all hosts',
    'hosts' => array (
        array('Redmine' => 'CPU Load'),
        array('Redmine' => 'CPU utilization'),
        array('test123' => 'Kernel process..'),
        array('test123' => 'Memory used')
    )
);

print_r($mArray);

$newArray = $mArray;
$newArray['hosts'] = array();

foreach($mArray['hosts'] as $currentHost) {
    foreach($currentHost as $hostKey => $hostValue) {
        $newArray['hosts'][$hostKey][] = $hostValue;
    }
}

print_r($newArray);

结果是:

Array
(
    [charts_group_name] => all hosts
    [hosts] => Array
        (
            [0] => Array
                (
                    [Redmine] => CPU Load
                )
            [1] => Array
                (
                    [Redmine] => CPU utilization
                )
            [2] => Array
                (
                    [test123] => Kernel process..
                )
            [3] => Array
                (
                    [test123] => Memory used
                )
        )
)
Array
(
    [charts_group_name] => all hosts
    [hosts] => Array
        (
            [Redmine] => Array
                (
                    [0] => CPU Load
                    [1] => CPU utilization
                )
            [test123] => Array
                (
                    [0] => Kernel process..
                    [1] => Memory used
                )
        )
)

如果您必须在数组中拥有Redminetest123,那么您应首先检查它们是否已存在于hosts子阵列中。您可以使用that one之类的递归函数来完成此操作。

答案 1 :(得分:0)

<?php
$multiplehostconfigarray = array(
    'charts_group_name' => 'all hosts',
    'hosts' => array(
                    array('Redmine' => 'CPU load'), 
                    array('Redmine' => 'CPU utilization'),
                    array('test123' => 'Kernel Process Creations'),
                    array('test123' => 'Memory used')
                )

     );


$array = array();
foreach($multiplehostconfigarray['hosts'] as $item)
{
      $array[key($item)][] = current($item);
} 

$multiplehostconfigarray['hosts'] = $array;
echo '<pre>';
print_r($multiplehostconfigarray);

答案 2 :(得分:0)

试试这个:

<?php
$arr = array(
    'charts_group_name' => 'all hosts',
    'hosts' => array (
        array('Redmine' => 'CPU load'),
        array('Redmine' => 'CPU utilization'),
        array('test123' => 'Kernel Process Creations'),
        array('test123' => 'Memory used'),
    )
);

$newArr = array_map(function($v) {
                        return is_array($v)
                                 ? call_user_func_array('array_merge_recursive', $v)
                                 : $v;
                    },
                    $arr);
print_r($newArr);

输出结果为:

Array
(
    [charts_group_name] => all hosts
    [hosts] => Array
        (
            [Redmine] => Array
                (
                    [0] => CPU load
                    [1] => CPU utilization
                )

            [test123] => Array
                (
                    [0] => Kernel Process Creations
                    [1] => Memory used
                )

        )

)