duanke6057
2017-09-12 14:00
浏览 97
已采纳

php foreach组元素具有相同的值

I have an array that looks like this:

array (size=6)
  0 => 
    array (size=3)
      'name' => string 'Umber' 
      'reason' => string 'No data' 
      'id' => string '12'
  1 => 
    array (size=3)
      'name' => string 'Jakar' 
      'reason' => string 'Wrong format' 
      'id' => string '12'
  2 => 
    array (size=3)
      'name' => string 'Lane'
      'reason' => string 'No data'
      'id' => string '12' 
  3 => 
    array (size=3)
      'name' => string 'Jake' 
      'reason' => string 'Not found' 
      'id' => string '13' 
  4 => 
    array (size=3)
      'name' => string 'Jame' 
      'reason' => string 'Wrong name'
      'id' => string '13'
  5 => 
    array (size=3)
      'name' => string 'Joe' 
      'reason' => string 'No data' 
      'id' => string '13' 

What I want to do is group these elements in a table row if the same id value:

12 | No data, wrong format, No data
13 | Not found, Wrong name, No data

I know I have to use foreach for this one but the logic for grouping these elements in a single row is beyond me. Any help would be appreciated. Thanks in advance. I've started with this.

foreach($a as $value){
echo $value['id'] . ' ' . $value['reason'];
}

图片转代码服务由CSDN问答提供 功能建议

我有一个如下所示的数组:

  array  (size = 6)
 0 =>  
 array(size = 3)
'name'=> 字符串'Umber'
'reason'=> 字符串'无数据'
'id'=> 字符串'12'
 1 =>  
 array(size = 3)
'name'=> 字符串'Jakar'
'reason'=> 字符串'格式错误'
'id'=> 字符串'12'
 2 =>  
 array(size = 3)
'name'=> 字符串'Lane'
'reason'=> 字符串'无数据'
'id'=> 字符串'12'
 3 =>  
 array(size = 3)
'name'=> 字符串'Jake'
'reason'=> 字符串'未找到'
'id'=> 字符串'13'
 4 =>  
 array(size = 3)
'name'=> 字符串'Jame'
'reason'=> 字符串'错误名称'
'id'=> 字符串'13'
 5 =>  
 array(size = 3)
'name'=> 字符串'Joe'
'reason'=> 字符串'无数据'
'id'=> 字符串'13'
   
 
 

我想要做的是如果相同的id值将这些元素分组在表格行中:

  12 | 没有数据,格式错误,没有数据
13 | 找不到,错误的名字,没有数据
   
 
 

我知道我必须将foreach用于这个,但是将这些元素分组在一行中的逻辑超出了我的范围。 任何帮助,将不胜感激。 提前致谢。 我已经开始了。

  foreach($ a as $ value){
echo $ value ['id']。  ''。  $ value ['reason']; 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drouie2014 2017-09-12 14:04
    已采纳

    First group elements to subarrays, then output each subarray:

    $groups = [];
    foreach($a as $value){
        if (!isset($groups[$value['id']])) {
            $groups[$value['id']] = [];
    
            // update
            $groups[$value['id']] = [
                'names' => [],
                'reasons' => [],
            ];
        }
        $groups[$value['id']][] = $value['reason'];
    
        // update
        $groups[$value['id']]['names'][] = $value['name'];
        $groups[$value['id']]['reasons'][] = $value['reason'];
    }
    
    foreach ($groups as $key => $value) {
        echo $key . ' | ' . implode(', ', $value);
    
        // update
        echo $key . ' | ' . implode(', ', $value['names']) . ' | ' . implode(', ', $value['reasons']);
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drp935159 2017-09-12 14:04

    Something like this, perhaps. It should help get you started:

    $output = [];
    foreach ($arrary as $arr) {
        if (!isset($output[$arr['id']])) {
            $output[$arr['id']] = [];
        }
    
        $output[$arr['id']][] = $arr['reason'];
    }
    
    foreach ($output as $k => $v) {
        echo $k;
        echo implode(',', $v);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题