-4
Array
(
[0] => Array
    (
        [id] => 1
        [name] => abc
        [email] => abc@abc.com
        [hardware_type] => keybord
    )

[1] => Array
    (
        [id] => 1
        [name] => abc
        [email] => abc@abc.com
        [hardware_type] => mouse
    )

)

和需要这样的结果

Array
(
[0] => Array
    (
        [id] => 1
        [name] => abc
        [email] => abc@abc.com
        [hardware_type] => keybord , mouse
    )

 )
4

2 回答 2

0

考虑:

$a; // array input 1
$b; // array input 2

这是您正在寻找的功能:

function datFunction($a, $b) {
    $c = array(); // array output
    foreach ($a as $key => $value) {
        if (array_key_exists($key, $b) and $b[$key] != $value) {
            $c[$key] = array($value, $b[$key]);
            // or $c[$key] = "$value , {$b[$key]}";
        } else {
            $c[$key] = $value;
        }
    }
    foreach ($b as $key => $value) {
        if (!array_key_exists($key, $c)) {
            $c[$key] = $value;
        }
    }
    return $c;
}
于 2012-12-01T05:33:13.443 回答
0
$array1 = array(array
    (
        'id' => 1,
        'name' =>' abc',
        'email' => 'abc@abc.com',
        'hardware_type' => 'keybord'
    ),array
    (
        'id' => 1,
        'name' => 'abc',
        'email' => 'abc@abc.com',
        'hardware_type' => 'mouse'
    )
    ,array
    (
        'id' => 2,
        'name' => 'abc2',
        'email' => 'abc@abc.com',
        'hardware_type' => 'mouse'
    )
);

function mergarrays($array){            
        $arrayout = array();
        for($i = 0; $i < count($array); ++$i){
        foreach($array[$i] as $key => $value){
                if(array_key_exists($key,$arrayout)){
                    if( strpos($arrayout[$key],$value) == null){
                        if($arrayout[$key] != $value)
                            {
                                if(!in_array($value,explode(',',$arrayout[$key]))){
                                    $arrayout[$key] = $arrayout[$key] . ','.$value;
                                    }                           
                            }                       
                        }                   
                }else
                {
                    $arrayout[$key] = $value ;
                }
            }
        }
        return $arrayout;
        }
print_r(mergarrays($array1));
于 2012-12-01T06:56:33.453 回答