基于每个数组中的单个值对PHP数组元素进行分组

时间:2017-11-01 14:28:32

标签: php arrays grouping

我目前有一个看起来像这样的数组:

  Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Test 1
            [age] => 42
            [another_id] => 5
        )

    [1] => Array
        (
             [id] => 2
            [name] => Test 2
           [age] => 47
            [another_id] => 3
        )

    [2] => Array
        (
             [id] => 3
            [name] => Test 3
            [age] => 30
            [another_id] => 2
        )
   [3] => Array
        (
             [id] => 4
            [name] => Test 7
            [age] => 60
            [another_id] => 3
        )
   [4] => Array
        (
             [id] => 5
            [name] => Test 10
            [age] => 38
            [another_id] => 3
        )

)

我要做的是获取所有数组项并打印出项目的报告,但是按“another_id”字段对它们进行分组。 例如,我正在寻找的结果是:

REPORT
    **Another id (2)**
    Name: Test 3

    **Another id (3)** 
    Name: Test 2
    Name: Test 7
    Name: Test 10

    **Another id (5)** 
    Name: Test 1

我可以将这些项目组合在一起,但它们都保留在一个数组中,我似乎无法将它们彼此分开以生成报告

  $grouped_types = array();

  foreach($invalid_results as $type){
      $grouped_types[$type['another_id']][] = $type;
  }
  print_r($grouped_types);

任何人都可以帮助我吗?非常感谢!

3 个答案:

答案 0 :(得分:1)

我认为你想做的是:

$grouped_types = array();

foreach($invalid_results as $type){
  $grouped_types[$type['another_id']][] = $type['name'];
}
var_dump($grouped_types);

输出:

array (size=3)
  5 => 
    array (size=1)
      0 => string 'Test 1' (length=6)
  3 => 
    array (size=3)
      0 => string 'Test 2' (length=6)
      1 => string 'Test 7' (length=6)
      2 => string 'Test 10' (length=7)
  2 => 
    array (size=1)
      0 => string 'Test 3' (length=6)

答案 1 :(得分:1)

首先创建一个数组以按另一个ID分组

$grouped_types = array();
foreach($invalid_results as $type){
       $grouped_types[$type['another_id']]['name'][] = $type['name']; 
}
ksort($grouped_types); // sort by another id

显示为报告

foreach($grouped_types as $key=>$value){
    echo "Another id ".$key."\n";
    foreach($value['name'] as $k=>$v){
        echo "Name: ".$v."\n";
    }
    echo "\n";
}

Out put:

Another id  2
Name:  Test 3

Another id  3
Name:  Test 2
Name:  Test 7
Name:  Test 10

Another id  5
Name:  Test 1

使用此代码添加任意数量的字段并显示它们。

$grouped_types = array();

foreach($invalid_results as $type){
    // add any number of fields here
    $grouped_types[$type['another_id']][] = array('name'=>$type['name'],'age'=>$type['age']);  

}

ksort($grouped_types);
print_r($grouped_types);
foreach($grouped_types as $key=>$value){
    echo "Another ".$key."\n";
    foreach($value as $k=>$v){
       foreach($v as $g=>$r){
           echo $g." ".$r.",";
       }
        echo "\n";
    }
    echo "\n";
}

答案 2 :(得分:1)

你实际上非常接近你的分组逻辑。如果您还没有为当前' another_id'创建分组,则只需添加一个部分即可。然而。然后,输出真的像几个嵌套循环一样简单,以遍历每个分组,然后遍历组中的每个项目。

$grouped_types = array();

foreach($invalid_results as $type){
    if(isset($grouped_types[$type['another_id']]))
    {
        $grouped_types[$type['another_id']][] = $type;
    }
    else
    {
        $grouped_types[$type['another_id']] = array($type);
    }
}

foreach($grouped_types as $key => $array)
{
    echo "**Another id (" . $key . ")**<br/>"; // if you're not outputting to HTML, use \n instead of <br/>

    foreach($array as $type)
    {
        echo "Name: " . $type["name"] . "<br/>";// if you're not outputting to HTML, use \n instead of <br/>
    }

    echo "<br/>";
}

DEMO