2016-05-17 208 views
1

我有一个空数组排序阵列

$stack = array(); 

现在我想填补它与在变量值我posted从一个form

$aV = $_POST['aValue']; 
$aP = $_POST['aPercent']; 

$bV = $_POST['bValue']; 
$bP = $_POST['bPercent']; 

$cV = $_POST['cValue']; 
$cP = $_POST['cPercent']; 

$dV = $_POST['dValue']; 
$dP = $_POST['dPercent']; 

另一个文件在得到了我想要的值put后,他们在stack阵列中。 并在最后我想call数组名称/值

$stack[0].'<br />'.$stack[1].'<br />'.$stack[2].'<br />'.$stack[3]; 

问题是我不能fiure了我如何把它们排列在正确 我试图像这样

$stackA = array(); 
$stackB = array(); 
$stackC = array(); 
$stackD = array(); 


$stackA[$aP] = $aV; 
$stackB[$bP] = $bV; 
$stackC[$cP] = $cV; 
$stackD[$dP] = $dV; 

$arr3 = $stackA + $stackB + $stackC + $stackD; 

但与此问题是,它将取代密钥(例如$arr3= 'blue' => 24将成为[0] => 24我需要得到[0] => [blue] => 24因此,当我拨打$arr3[0]它会说blue 24,当[1] => [red] = 20我想第一个([0]到是最低的数字,在这种情况下是[red])我试过使用sort()函数。但在排序和name没有对number

+0

不应该'$ AV = $ _ POST [ 'aPercent'];'是'$ AV = $ _ POST ['安勤];'?用'$ bP','$ bV'等等......? –

+0

哦,真是愚蠢。但是这不影响代码。它只是一个愚蠢的错字 –

+0

你知道如何解决这个问题@PeterDarmis?例如, –

回答

1

试试这个:

//Put values in a stack with number as keys 
$stack[$aV] = $aP; and so on for others. 

//Sort based on keys.(eg: 24, 20 in your case) 
$stack = ksort($stack, SORT_NUMERIC); 

// iterate and push all of them into $arr3 in the format you need. 
foreach($stack as $V => $P) { 
    $arr3[] = array($V => $P); 
} 

print_r($arr3); 
+0

OP为什么要“尝试这个”?一个**好的答案**总是会解释做了什么以及为什么这样做, 不仅适用于OP,而且适用于SO的未来访问者,可能会发现此问题并正在阅读您的答案。 – RiggsFolly

+0

@RiggsFolly认为这是自我解释,更新了一些意见,以便更清楚地理解。 – jitendrapurohit