0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHP:array_mapの中の配列の要素がさらに配列で、その配列は処理を入れると構成を様変わりすらできる

Last updated at Posted at 2023-02-26

array_mapの中の配列の要素がさらに配列になっているとき、その配列は処理を入れると構成を様変わりすらできてしまう。
array_map、無名関数、アロー関数、array_chunkを混ぜ合わせるとすごい。
いくらでもやりようがあり、便利極まりない。


処理前
[index0][0] => [index0]['grape5', 'apple2']
[index1][1] => [index1]['lemmon3', 'orange3']

処理後
[index0][0] => ['fruit' => 'sweet set', 'quantity' => 7, 'price' => 500, 'rank' => 5]
[index1][1] => ['fruit' => 'citrus set', 'quantity' => 5, 'price' => 400, 'rank' => 3]

こういったことができてしまう。

function getResult(string $fruitA1, string $fruitA2, string $fruitB1, string $fruitB2): array {
    $fruitRanks = convertToFruitRanks([$fruitA1, $fruitA2, $fruitB1, $fruitB2]);
    $customerfruitRanks = array_chunk($fruitRanks, 2);
    $orders = array_map(fn ($customerfruitRank) => searchOrder($customerfruitRank[0], $customerfruitRank[1]), $customerfruitRanks);
}

searchOrder関数で処理後の配列構成へ変容させられる。あまりにも便利。
array_chunkも前後で二分して配列化できて便利すぎる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?