根据共享列值将二维数组中的行推送到另一个二维数组的新子数组中

问题描述 投票:0回答:3

我想以分组方式将一个数组的行合并到另一个数组中。

$users = [
    ['email' => '[email protected]', 'state' => 'IL'],
    ['email' => '[email protected]', 'state' => 'HI'],
];

$jobs = [
    ['title' => 'Marketing Coordinator', 'location' => 'Chicago', 'state' => 'IL'],
    ['title' => 'Sales Manager (MA)', 'location' => 'Springfield', 'state' => 'IL'],
    ['title' => 'Security Guard/Driver', 'location' => 'Big Island', 'state' => 'HI'],
    ['title' => 'Directory of Sales and Operation Planning', 'location' => 'Honolulu', 'state' => 'HI'],
    ['title' => 'Associate, Strategic Finance', 'location' => 'Joliet', 'state' => 'IL'],
];

我正在尝试根据共享的“状态”值组合两个数组。

我想要的输出是:

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [state] => IL
            [jobs] => Array
            (
                    [0] => Array
                    (
                            [title] => Marketing Coordinator
                            [location] => Chicago
                            [state] => IL
                    )

                    [1] => Array
                    (
                            [title] => Sales Manager (MA)
                            [location] => Springfield
                            [state] => IL
                    )
                    [2] => Array
                    (
                            [title] => Associate, Strategic Finance
                            [location] => Joliet
                            [state] => IL
                    )
                ) 
        )

    [1] => Array
        (
            [email] => [email protected]
            [state] => HI
            [jobs] => Array
            (
                    [0] => Array
                    (
                            [title] => Security Guard/Driver
                            [location] => Big Island
                            [state] => HI
                    )

                    [1] => Array
                    (
                            [title] => Director of Sales and Operation Planning
                            [location] => Honululu
                            [state] => HI
                    )
                ) 
        )
)
php arrays multidimensional-array mapping grouping
3个回答
2
投票

首先按州索引作业数组。 (不这样做也可以实现你的目标,但这会让事情变得更容易。)

foreach ($jobs as $job) {
    $jobs_by_state[$job['state']][] = $job;
}

然后迭代用户数组并附加与每个用户状态对应的作业集。

foreach ($users as &$user) {
    $user['jobs'] = $jobs_by_state[$user['state']];
}

工作示例:https://3v4l.org/hSY4G

根据您要对这些进行的操作,仅执行第一部分(按状态对作业进行分组)并将两个数组分开,直到准备好呈现为止,这可能是有意义的。通过按状态索引的作业数组,将更容易在表示层中引用,因此您可以减少用户数据中不必要的重复。


1
投票

例如这样:

// loop through all jobs foreach($jobs as $job){ //loop through all users and get the current user element as reference (& parameter), so when $user is manipulated, the original element is changed, not a copy (as in a "normal" foreach loop) foreach($users as &$user) { // if the state of the current user matches the state of the current job, it must be appended to the original user if($user['state'] === $job['state']){ // check, if jobs have already been appended to the user if(!isset($user['jobs'])) { // if not, we need to create the jobs key for this user and define a new, empty array $user['jobs'] = []; } // finally, append the job to the original user $user['jobs'][] = $job; } } }


                

0
投票
$users

数组应将

jobs
元素定义为引用,然后迭代
$jobs
数组以将其行推入特定于状态的
jobs
引用。
代码:(

演示

foreach ($users as ['state' => $state, 'jobs' => &$ref[$state]]); foreach ($jobs as $job) { $ref[$job['state']][] = $job; } var_export($users);

© www.soinside.com 2019 - 2024. All rights reserved.