2

我有一个动态的 $my_Array。我应该怎么做才能根据 $my_Array 获得 $Final_array。

    $my_Array = array(  
                ['arm'] => 'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6',
                ['min'] => 'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6',
                ['msk'] => 'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6',
                ['con'] => 'c72a7fb8-308a-482a-96d5-7a7853e16c49',
                ['ref'] => 'c72a7fb8-308a-482a-96d5-7a7853e16c49',
             ) 


   $Final_array = array
                (   'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6' => 'arm, min, msk',
                    'c72a7fb8-308a-482a-96d5-7a7853e16c49' => 'con, ref'
                )
4

3 回答 3

3
$my_Array = array(  
                'arm' => 'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6',
                'min' => 'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6',
                'msk' => 'cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6',
                'con' => 'c72a7fb8-308a-482a-96d5-7a7853e16c49',
                'ref' => 'c72a7fb8-308a-482a-96d5-7a7853e16c49',
             ) ;
$data = array();
foreach( $my_Array as $key => $value) {
    if(isset( $data[ $value ] )) {
        $data[ $value ] = $data[ $value ] .', '.$key;
    } else {
        $data[ $value ] = $key;
    }
}

希望上面的代码对您有所帮助。

于 2013-07-18T10:06:39.423 回答
1
$Final_array = array();               
foreach($my_Array as $key => $value){
 if(array_key_exists($value, $Final_array)){

  $Final_array[$value] = $Final_array[$value]. ", ".$key;

 }else{

  $Final_array[$value] = $key;

 }
}
print_r($Final_array);        
?>

输出:

Array
(
    [cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6] => arm, min, msk
    [c72a7fb8-308a-482a-96d5-7a7853e16c49] => con, ref
)
于 2013-07-18T10:05:51.727 回答
0

我试图用一个循环来做到这一点,但失败了,但希望你没问题。

$new = array();
foreach ($my_Array as $key => $val) {
   $new[$val][] = $key;
}

foreach ($new as $key => $val) {
   $new[$key] = implode(", ". $val);
}
于 2013-07-18T10:02:06.760 回答