1, "id" => 1, "index" => 1, "amount" => 50000), (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000), (2) => array("level" => 1,"> 1, "id" => 1, "index" => 1, "amount" => 50000), (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000), (2) => array("level" => 1,"> 1, "id" => 1, "index" => 1, "amount" => 50000), (1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000), (2) => array("level" => 1," />

# どのようにマージし、PHPの配列をカスタムまたはユーザー定義の基準で並べ替える？

``````\$data1 = array(
(0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
(1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
(2) => array("level" => 1, "id" => 3, "index" => 1, "amount" => 0));

\$data2 = array(
(0) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
(1) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
(2) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
(3) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000),
(4) => array("level" => 2, "id" => 3, "index" => 1, "amount" => 0));
``````

これらの配列を1つの配列にマージしたいのですが、その結果は次のようになります：

``````\$expected = array(
(0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
(1) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
(2) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
(3) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
(4) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
(5) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000));
``````

``````\$try = array_merge_recursive(\$data1, \$data2);
``````

しかし結果は次のようなものです：

``````\$try = array(
(0) => array("level" => 1, "id" => 1, "index" => 1, "amount" => 50000),
(1) => array("level" => 1, "id" => 2, "index" => 1, "amount" => 40000),
(3) => array("level" => 1, "id" => 3, "index" => 1, "amount" => 0),
(4) => array("level" => 2, "id" => 1, "index" => 1, "amount" => 30000),
(5) => array("level" => 2, "id" => 1, "index" => 2, "amount" => 20000),
(6) => array("level" => 2, "id" => 2, "index" => 1, "amount" => 15000),
(7) => array("level" => 2, "id" => 2, "index" => 2, "amount" => 25000),
(8) => array("level" => 2, "id" => 3, "index" => 1, "amount" => 0));
``````

1. 量= 0の配列を削除する
2. ID、レベル、インデックスによる並べ替えと並べ替え

0

## 2 答え

``````\$data = array_merge(\$data1, \$data2);
\$data = array_filter(\$data, function (\$d) {
return \$d['amount'] != 0;
});
usort(\$data, function (\$a, \$b) {
if (\$a['id'] != \$b['id'])       return \$a['id'] - \$b['id'];
if (\$a['level'] != \$b['level']) return \$a['level'] - \$b['level'];
else                            return \$a['index'] - \$b['index'];
});
``````

3

それは完璧に動作し、多くの@decezeに感謝！

これはあなたのために働きますか？

```\$finalArr = array_merge(\$data1,\$data2);

foreach(\$finalArr as \$key => \$val) {
if(empty(\$val['amount'])) {
unset(\$finalArr[\$key]);
}
}
echo "";
sort(\$finalArr);
print_r(\$finalArr);

Hope it helps
```
1

それは動作しますが、空の量を削除するためだけに、` id、level、index `でソートして並べ替えません。とにかく助けてくれてありがとう。

PHP - 日本のコミュニティ [ja]
4 参加者の

このグループではPHPについて話します。 パートナー：kotaeta.com