0

i have an array like below i want to merge arrays with the same keys

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
        )
    )
)

but i want like below,i mean i want to merge keys with the same name like 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
           )
        )
    )
)

i tried this code..but i am not getting the answer

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 );
    } `
}
Stanimir Stoyanov
  • 1,623
  • 18
  • 29

3 Answers3

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);

The result of that is:

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
                )
        )
)

If you must have Redmine and test123 within array themself, then you should first check if they already exists within hosts subarray. You can do this by using recursive function like that one.

Community
  • 1
  • 1
Stanimir Stoyanov
  • 1,623
  • 18
  • 29
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);
Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
0

Try this:

<?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);

The output is:

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
                )

        )

)
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15