【问题标题】:PHP efficient way to combine two associative arrays into one multidimensional associative arrayPHP将两个关联数组组合成一个多维关联数组的有效方法
【发布时间】:2021-01-04 20:51:29
【问题描述】:

这是第一个问题:

我有两个关联数组,一个包含销售人员,一个包含客户。

  $salesPersons = array(
    array(
      "id" => "1",
      "name" => "Mr Smith",
      "email" => "mrsmith@email.com",
      "clients" => array()
    ),
    array(
      "id" => "2",
      "name" => "James Bond",
      "email" => "jamesbond@email.com",
      "clients" => array()
    )
  );

  $clients = array(
    array(
      "id" => "1",
      "name" => "Lucifer Enterprises",
      "salesPersonId" => "1"
    ),
    array(
      "id" => "2",
      "name" => "Charlies Chocolate Factory",
      "salesPersonId" => "1"
    ),
    array(
      "id" => "3",
      "name" => "Geckos Investments",
      "salesPersonId" => "2"
    ),
  );


我想通过 ID 将 $salesPersons['id'] 映射到 clients['salesPersonId'] 并返回一个多维关联数组,如下所示:

  $result_i_want = array(
    array(
      "id" => "1",
      "name" => "Mr Smith",
      "email" => "mrsmith@email.com",
      "clients" => array(
        array(
          "id" => "1",
          "name" => "Lucifer Enterprises",
        ),
        array(
          "id" => "2",
          "name" => "Charlies Chocolate Factory",
        ),
      )
    ),
    array(
      "id" => "2",
      "name" => "James Bond",
      "email" => "jamesbond@email.com",
      "clients" => array(
        array(
          "id" => "3",
          "name" => "Geckos Investments",
        ),
      )
    )
  );

我对第一个问题的解决方案

我已经使用嵌套的 foreach-loops 和 array_push 解决了这个问题

  $result = array();

  foreach ($clients as $c_record) {
    foreach ($salesPersons as $s_record) {
      if ($c_record['salesPersonId'] == $s_record['id']) {
        array_push($s_record['clients'], array(
          "id" => $c_record['id'],
          "name" => $c_record['name']
        ));
        array_push($result, $s_record);
      }
    }
  }

剩下的问题

这个解决方案似乎效率不高。 对于每个客户记录,我都会检查所有销售人员以查看是否匹配。我认为计算的数量是:

no. of clients * no. of sales persons

我有一个庞大的数据库,还需要通过将项目映射到客户并将可交付成果映射到项目来添加更多维度。我认为这可能会造成问题。

问题

有没有更有效的方法来获得相同的结果?

【问题讨论】:

  • 这些数组来自哪里?这两个是独立的查询结果吗?
  • 这是来自 Web 应用程序 API 的两个单独的 GET 请求。我得到我解码为关联数组的 JSON 数据

标签: php arrays


【解决方案1】:

建立索引:
您需要通过 id 访问您的 salesPerson 条目,您可以首先创建一个关联数组 id => salesPerson,然后在循环中使用此关联数组。

$salesById = array();

foreach ($salesPersons as $s_record) {
    $salesById[ $s_record['id'] ] = $s_record;
}

$result = array();

foreach ($clients as $c_record) {
    $s_record = $salesById[ $c_record['salesPersonId'] ];

    if ($s_record == null) {
        // you may want to handle invalid ids in the client array
        // one way is to simply ignore this client record :
        continue;
    }

    array_push($s_record['clients'], array(
      "id" => $c_record['id'],
      "name" => $c_record['name']
    ));
    array_push($result, $s_record);
}

注意事项

您创建$result 数组的方式可能存在问题:
如果销售人员有 n 客户,$result 数组将引用该销售人员 n 次。

仔细查看您真正想要的结果,您可能只想返回$salesPersons$salesById

【讨论】:

  • 正如您所指出的,上述解决方案会导致结果数组中出现重复的 salesPersons,每个客户都有一个客户。我正在寻找与一个销售人员可以拥有多个客户的问题相同的结果。我会尝试调整您的代码,但如果您对此有解决方案,请分享以便可以回答问题
  • 最后一句:“您可能只想返回 $salesPersons 或 $salesById。”
【解决方案2】:

正如 LeGEC 所说:

您需要通过 id 访问您的 salesPerson 条目,您可以先创建一个关联数组 id => salesPerson,然后在循环中使用这个关联数组。

您需要使用 id 设置数组的索引:

<?php
$salesPersons = array(
    1 => array(
        "id" => "1",
        "name" => "Mr Smith",
        "email" => "mrsmith@email.com",
        "clients" => array()
    ),
    2 => array(
        "id" => "2",
        "name" => "James Bond",
        "email" => "jamesbond@email.com",
        "clients" => array()
    )
);

$clients = array(
    1 => array(
        "id" => "1",
        "name" => "Lucifer Enterprises",
        "salesPersonId" => "1"
    ),
    2 => array(
        "id" => "2",
        "name" => "Charlies Chocolate Factory",
        "salesPersonId" => "1"
    ),
    3 => array(
        "id" => "3",
        "name" => "Geckos Investments",
        "salesPersonId" => "2"
    ),
);

然后:

$result = array();
foreach ($clients as $id => $c_record) {
    if (isset($salesPersons[$id])) {
        $result[] = array_merge($clients[$id], $salesPersons[$id]);
    } else {
        $result[] = $clients[$id];
    }
}
var_dump($result);

这里的结果:http://sandbox.onlinephpfunctions.com/code/e590bdb5aaea2794fc5a04ee60f61db766129664

PS:

我的代码适用于您的用例,但如果 $salesPersons 数组的大小大于 $clients 数组,它将无法正常工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 2011-11-26
    • 1970-01-01
    • 2012-10-21
    • 2013-03-09
    • 2019-08-11
    相关资源
    最近更新 更多