在foreach中创建项目并创建数组

时间:2015-05-11 07:30:02

标签: php mysql arrays foreach

在下面的示例中,我循环查看具有以下输出的MySQL查询结果:

enter image description here

这是我到目前为止的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
    )
  ...................
)

如何达到上述效果?

2 个答案:

答案 0 :(得分:0)

你可以这样做 -

$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'];
      }
   }
}

答案 1 :(得分:0)

<?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>';