我有一个空数组
$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];
问题是我无法确定如何正确地将它们放入阵列中 我试过这样的
$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 :(得分: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);