【问题标题】:Group items in foreach and create array在 foreach 中对项目进行分组并创建数组
【发布时间】:2015-07-21 14:49:14
【问题描述】:

在下面的示例中,我正在遍历具有以下输出的 MySQL 查询的结果:

这是我目前拥有的 PHP:

foreach ($customers as $customer) {

  if($customer['innumber'] != null){

      $chartInbound['name'] = $customer['name'];
      $chartInbound['label'] = $customer['innumber'];
      $chartInbound['count'] = $customer['count'];
      $chartInbound['customerid'] = $customer['id'];

      array_push($out['chartInbound'], $chartInbound);
   }
}

print_r($out['chartInbound']); 的输出为:

Array
(
[0] => Array
    (
        [name] => 1st Online Solutions
        [label] => 01-02
        [count] => 577
        [customerid] => 129
    )

[1] => Array
    (
        [name] => Bookngo
        [label] => 01-02
        [count] => 2
        [customerid] => 95
    )

[2] => Array
    (
        [name] => Boutixury
        [label] => 07
        [count] => 1
        [customerid] => 14
    )

[3] => Array
    (
        [name] => Cruise Village
        [label] => 01-02
        [count] => 16
        [customerid] => 25
    )

[4] => Array
    (
        [name] => Cruise Village
        [label] => 00
        [count] => 1
        [customerid] => 25
    )

[5] => Array
    (
        [customer] => Cruise Village
        [label] => 07
        [countInbound] => 16
        [minsInbound] => 125
        [customerid] => 25
    )
  ...................
)

所需的输出应该是:

Array
(
[0] => Array
    (
        [name] => 1st Online Solutions
        [01-02] => 577
        [customerid] => 129
    )

[1] => Array
    (
        [name] => Bookngo
        [01-02] => 2
        [customerid] => 95
    )

[2] => Array
    (
        [name] => Boutixury
        [07] => 1
        [customerid] => 14
    )

[3] => Array
    (
        [name] => Cruise Village
        [07] => 16
        [00] => 1
        [01-02] => 16
        [customerid] => 25
    )
  ...................
)

我怎样才能达到上述结果?

【问题讨论】:

标签: php mysql arrays foreach


【解决方案1】:

你可以这样做 -

$chartInbound = array(); 
foreach ($customers as $customer) {

  if($customer['innumber'] != null){
      if(array_key_exists([$customer['id']], $chartInbound)) {
         $chartInbound[$customer['id']][$customer['innumber'] = $customer['count'];
      } else {
         $chartInbound[$customer['id']]['name'] = $customer['name'];
         $chartInbound[$customer['id']][$customer['innumber'] = $customer['count'];
         $chartInbound[$customer['id']]['customerid'] = $customer['id'];
      }
   }
}

【讨论】:

  • 您还在使用array_push()吗?
【解决方案2】:
<?php
$customers = array(
    array('name' => '1st Online Solutions', 'innumber' => '01-02', 'count' => 577, 'id' => 129),
    array('name' => 'Bookngo', 'innumber' => '01-02', 'count' => 2, 'id' => 95),
    array('name' => 'Boutixury', 'innumber' => '07', 'count' => 1, 'id' => 14),
    array('name' => 'Cruise Village', 'innumber' => '01-02', 'count' => 16, 'id' => 25),
    array('name' => 'Cruise Village', 'innumber' => '00', 'count' => 1, 'id' => 25)
);
$out = array('chartInbound' => array());
$ids = array(); // Stores the customer keys to quickly search if a customer has already been added
foreach ($customers as $customer) {

    if ($customer['innumber'] != null) {
        $customerID = $customer['id'];

        if (($pos = array_search($customerID, $ids)) !== false) { // To check if the customer is already added
            $out['chartInbound'][$pos][$customer['innumber']] = $customer['count'];
        } else {
            $chartInbound = array();
            $chartInbound['customerid'] = $customerID;
            $chartInbound['name'] = $customer['name'];
            $chartInbound[$customer['innumber']] = $customer['count'];
            $out['chartInbound'][] = $chartInbound;
            $ids[] = $customerID;
        }

    }
}
echo '<pre>';
print_r($out['chartInbound']);
echo '</pre>';

【讨论】:

  • 这解决了一个问题,但是对于所有号码类型的每个客户都应该只有一个条目,就像我想要的示例中一样
  • @alex 我已经更新了我的答案。请检查这是否适合您的需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-25
  • 1970-01-01
  • 2019-06-28
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多