【问题标题】:Php Loop logic with repeated values具有重复值的 PHP 循环逻辑
【发布时间】:2017-05-02 12:57:26
【问题描述】:

我需要找出一个让我感到困惑的逻辑。 我有一个看起来像这样的数组。

array (
    0 => array (
        'invoice_id' => 'WUI_588'
        'email' => 'abc@hotmail.com'
    ),
    1 => array (
        'invoice_id' => 'WUI_588'
        'email' => 'def@hotmail.com'
    ),
    2 => array (
        'invoice_id' => 'WUI_589'
        'email' => 'ninja@hotmail.com'
    ),
    3 => array (
        'invoice_id' => 'WUI_589'
        'email' => 'ghi@hotmail.com'
    ),
    4 => array (
        'invoice_id' => 'WUI_590'
        'email' => '123@hotmail.com'
    ),
    5 => array (
        'invoice_id' => 'WUI_590'
        'email' => '123@hotmail.com'
    ),
    6 => array (
        'invoice_id' => 'WUI_591'
        'email' => '456@hotmail.com'
    ),
    7 => array (
        'invoice_id' => 'WUI_591'
        'email' => '456@hotmail.com'
    ),
)

所以如果我要在表格布局中打印数组,我会得到这样的结果:-

INDEX | INVOICE ID | EMAIL 
  0   |   WUI_588  | abc@hotmail.com
  1   |   WUI_588  | def@hotmail.com
  2   |   WUI_589  | ninja@hotmail.com
  3   |   WUI_589  | ghi@hotmail.com

我想要的是这样的:-

INDEX | INVOICE ID | EMAIL 
  0   |   WUI_588  | abc@hotmail.com, def@hotmail.com
  1   |   WUI_589  | ninja@hotmail.com, ghi@hotmail.com

....等等。 我所做的尝试之一是这样的,

foreach ($array as $key => $value) {
  $invoiceidbucket[] = $value['invoice_id'];
  if(in_array($value['invoice_id'], $invoiceidbucket)) {
    $value['merged_email'][] = $value['email'];

    //Clearing array
    $invoiceidbucket = array;
  }
}

【问题讨论】:

  • 向我们展示一些您尝试过的代码或您第一次到达的代码
  • @Thamilan,我添加了一个不成功的解决方案。
  • 看看@Saty 的回答。他们在回答你之前打赌我:)
  • @Thamilan 你在用同样的逻辑吗??
  • @Saty,不。有些不同,但使用相同的内置方法

标签: php arrays loops logic


【解决方案1】:

使用以下代码创建所需的数组:-

$final_array = array();
foreach($initial_array as $arr){
  $final_array[$arr['invoice_id']]['invoice_id'] = $arr['invoice_id'];
  $final_array[$arr['invoice_id']]['email'] = (!empty($final_array[$arr['invoice_id']]['email']))?$final_array[$arr['invoice_id']]['email'].','.$arr['email'] : $arr['email'];
}
$final_array = array_values($final_array);
echo "<pre/>";print_r($final_array);

输出:- https://eval.in/785516

注意:- 现在在循环中使用此 $final_array (foreach()) 并以所需方式打印

【讨论】:

    【解决方案2】:

    这样做:

    $a = [];//Your array;
    $retArray = []; //Output Array
    foreach ($a as $key => $val) {
        $retArray['invoice_id'][] =  $retArray['email'];
    }
    

    在循环结束时,您将拥有所有 invoice_ids 作为键及其电子邮件 ID 数组。

    在此之后随心所欲地打印 :)

    【讨论】:

      【解决方案3】:

      我的方法不会进行不必要的迭代覆盖。我推荐这个:

      代码:(Demo)

      $array=[
          ['invoice_id'=>'WUI_588','email'=>'abc@hotmail.com'],
          ['invoice_id'=>'WUI_588','email'=>'def@hotmail.com'],
          ['invoice_id'=>'WUI_589','email'=>'ninja@hotmail.com'],
          ['invoice_id'=>'WUI_589','email'=>'ghi@hotmail.com'],
          ['invoice_id'=>'WUI_590','email'=>'123@hotmail.com'],
          ['invoice_id'=>'WUI_590','email'=>'123@hotmail.com'],
          ['invoice_id'=>'WUI_591','email'=>'456@hotmail.com'],
          ['invoice_id'=>'WUI_591','email'=>'456@hotmail.com']
      ];
      foreach($array as $row){  // iterate all rows
          if(!isset($result[$row['invoice_id']])){  // if first occurrence of invoice_id...
              $result[$row['invoice_id']]=$row;     // save the full row with invoice_id as the temporary key
          }else{                                    // if not the first occurrence of invoice_id...
              $result[$row['invoice_id']]['email'].=", {$row['email']}";  // concatenate the email value
          }
      }
      var_export(array_values($result));
      

      输出:

      array (
        0 => 
        array (
          'invoice_id' => 'WUI_588',
          'email' => 'abc@hotmail.com, def@hotmail.com',
        ),
        1 => 
        array (
          'invoice_id' => 'WUI_589',
          'email' => 'ninja@hotmail.com, ghi@hotmail.com',
        ),
        2 => 
        array (
          'invoice_id' => 'WUI_590',
          'email' => '123@hotmail.com, 123@hotmail.com',
        ),
        3 => 
        array (
          'invoice_id' => 'WUI_591',
          'email' => '456@hotmail.com, 456@hotmail.com',
        ),
      )
      

      【讨论】:

        【解决方案4】:

        在开始新行之前检查发票 ID 列循环。如果找到匹配项,请将电子邮件放在同一行中,并在前面添加一个逗号,以防它不是该行的第一个电子邮件 ID。

        可以向我们展示您是如何处理它的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-02-24
          • 1970-01-01
          • 2016-01-25
          • 1970-01-01
          • 2020-02-06
          • 2015-06-12
          • 1970-01-01
          相关资源
          最近更新 更多