对单词

时间:2016-05-17 09:29:16

标签: php arrays

我有一个空数组

$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

1 个答案:

答案 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);