Help us understand the problem. What is going on with this article?

PHP:連想配列をキーでグループ化する

More than 1 year has passed since last update.

例えば、こういう食べ物の連想配列を

<?php
$foods = [
    ['name' => 'Apple', 'category' => 'fruits'],
    ['name' => 'Strawberry', 'category' => 'fruits'],
    ['name' => 'Tomato', 'category' => 'vegetables'],
    ['name' => 'Carot', 'category' => 'vegetables'],
    ['name' => 'water', 'category' => 'drink'],
    ['name' => 'beer', 'category' => 'drink'],
];
?>

カテゴリごとにグループ化して、こんな感じにしたい:

<?php
$expected = [
    'fruits' => [
        ['name' => 'Apple', 'category' => 'fruits'],
        ['name' => 'Strawberry', 'category' => 'fruits'],
    ],
    'vegetables' => [
        ['name' => 'Tomato', 'category' => 'vegetables'],
        ['name' => 'Carot', 'category' => 'vegetables'],
    ],
    'drink' => [
        ['name' => 'water', 'category' => 'drink'],
        ['name' => 'beer', 'category' => 'drink'],
    ],
];
?>

実装案1: foreachを使ったよくある実装

<?php

function array_group_by(array $items, $keyName)
{
    $groups = [];
    foreach ($items as $item) {
        $key = $item[$keyName];
        if (array_key_exists($key, $groups)) {
            $groups[$key][] = $item;
        } else {
            $groups[$key] = [$item];
        }
    }
    return $groups;
}

assert(array_group_by($foods, 'category') === $expected);
?>

更に短縮すると:

function group_by(array $table, string $key): array
{
    $groups = [];
    foreach ($table as $row) {
        $groups[$row[$key]][] = $row;
    }
    return $groups;
}

実装案2: イミュータブル風

<?php

function array_group_by2(array $items, $keyName)
{
    $keys = array_unique(array_column($items, $keyName));
    return array_combine($keys, array_map(function($key) use ($items, $keyName) {
        return array_values(array_filter($items, function ($item) use ($key, $keyName) {
            return $item[$keyName] == $key;
        }));
    }, $keys));
}

assert(array_group_by2($foods, 'category') === $expected);
?>

変数スコープとコレクションのAPI、辛すぎ・・・。やはりPHPはforeachを使うが良さそう。

実装案3: 先に入れ物を作ってforeachでミューテート

<?php

function array_group_by3(array $items, $keyName)
{
    $groups = array_fill_keys(array_column($items, $keyName), []);
    foreach ($items as $item) {
        $groups[$item[$keyName]][] = $item;
    }
    return $groups;
}

assert(array_group_by3($foods, 'category') === $expected);
?>

実装案1と比べて、if-elseが無くなった。まずまずと言った感じ・・・?

実装案4: array_reduce

function group_by(array $table, string $key): array
{
    return array_reduce($table, function (array $groups, array $row) use ($key) {
        $groups[$row[$key]][] = $row;
        return $groups;
    }, []);
}
suin
Qiita 4位/TypeScript入門書執筆中/TypeScripterのための座談会「YYTypeScript」主催/『実践ドメイン駆動設計』書籍邦訳レビュア/分報Slack考案/YYPHP主催/CodeIQマガジン執筆/株式会社クラフトマンソフトウェア創設/Web自動テスト「ShouldBee」の開発/TypeScript/DDD/OOP
https://yyts.connpass.com/
shouldbee
開発者向けテスト支援サービスShouldBeeを開発・運営するスタートアップ(onlab第8期)
http://shouldbee.at
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした