【发布时间】:2016-07-27 12:47:53
【问题描述】:
我有
$products = array(
"product1" => [
"a" => ["total" => 1],
"b" => ["total" => 3],
"c" => ["total" => 2],
],
"product2" => [
"d" => ["total" => 3],
"f" => ["total" => 2],
"e" => ["total" => 1],
],
"product3" => [
"g" => ["total" => 3]
],
);
这些是我的产品和每个仓库的库存(仓库 a 有 1 件 product1...)
我想按每种产品的库存对每个仓库进行排序。 我已经做到了:
foreach ($products as &$stocks) {
uasort($stocks, function($elmt1, $elmt2) {
return $elmt2["total"] - $elmt1["total"];
});
}
我在哪里打印我的新数组:
array(3) {
["product1"]=>
array(3) {
["b"]=>
array(1) {
["total"]=>
int(3)
}
["c"]=>
array(1) {
["total"]=>
int(2)
}
["a"]=>
array(1) {
["total"]=>
int(1)
}
}
["product2"]=>
array(3) {
["d"]=>
array(1) {
["total"]=>
int(3)
}
["f"]=>
array(1) {
["total"]=>
int(2)
}
["e"]=>
array(1) {
["total"]=>
int(1)
}
}
["product3"]=>
&array(1) {
["g"]=>
array(1) {
["total"]=>
int(3)
}
}
}
它完成了我的工作,但当我仔细观察时,我只能在其中一个数组中看到“&”字符。
为什么?
【问题讨论】:
-
预期结果是什么?
-
你会在这里找到一个很好的解释:stackoverflow.com/a/8220457/5349893
-
@Anant 我的预期结果将是没有数组中的引用。 Juste 数组本身。 Product3 是一个参考
标签: php pass-by-reference php-7