从MySQL连接查询中,我得到如下结果:
Array ( [0] => c1 [1] => ot1 [2] => ot1 [3] => R )
Array ( [0] => 20 [1] => 10 [2] => 15 [3] => 5 )
但是我想要这样的结果:
c1 = [20]
ot1 = [10,15]
R = [5]
请帮助我。
答案 0 :(得分:0)
假设
$array1 = array( 0 => 'c1', 1 => 'ot1', 2 => 'ot1' 3 => 'R' )
$array2 = array( 0 => 20, 1 => 10, 2 => 15, 3 => 5 )
尝试一下:
for($i = 0; $i < count($array1); $i++){
if(!isset(${$array1[$i]}))
${$array1[$i]} = [];
${$array1[$i]}[] = $array2[$i];
}
请注意,仅当键是从0开始的连续整数并且两个数组的维数相同时,此方法才有效
答案 1 :(得分:0)
假设第一个数组称为$array1
,另一个数组称为$array2
,请尝试以下操作:
$merged = []; //new array for merged values
foreach ($array1 as $key => $value) { //iterate over the array which holds keys
if (!isset($merged[$value])) { //if a key does not exist in new array yet
$merged[$value] = []; //add it
}
$merged[$value][] = $array2[$key]; //add new value from the other array under this key
}
只要保留数组的完整性,就不会像其他答案中那样对连续键有任何限制
答案 2 :(得分:0)
使用这个
UserControl1.ButtonClicked += (s, e) =>
{
//show usercontrol2 on your panel.
}