I have an $my_Array which is dynamic. What should i do to get $Final_array based on $my_Array.
我有一个动态的$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'
)
3 个解决方案
#1
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;
}
}
Hope this above code is help you.
希望以上代码能对您有所帮助。
#2
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);
?>
Output:
输出:
Array
(
[cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6] => arm, min, msk
[c72a7fb8-308a-482a-96d5-7a7853e16c49] => con, ref
)
#3
0
I tried to do this with one loop and failed, but hope its ok with you.
我试着用一个循环来做这件事,但失败了,希望你能接受。
$new = array();
foreach ($my_Array as $key => $val) {
$new[$val][] = $key;
}
foreach ($new as $key => $val) {
$new[$key] = implode(", ". $val);
}
#1
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;
}
}
Hope this above code is help you.
希望以上代码能对您有所帮助。
#2
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);
?>
Output:
输出:
Array
(
[cc9f8eb4-e29f-44b4-8dcd-ac08e9b1faa6] => arm, min, msk
[c72a7fb8-308a-482a-96d5-7a7853e16c49] => con, ref
)
#3
0
I tried to do this with one loop and failed, but hope its ok with you.
我试着用一个循环来做这件事,但失败了,希望你能接受。
$new = array();
foreach ($my_Array as $key => $val) {
$new[$val][] = $key;
}
foreach ($new as $key => $val) {
$new[$key] = implode(", ". $val);
}