1

I currently have an array which looks like:

  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
        )

)

What I am looking to do is take all of the array items and print out a report of the items but group them by the 'another_id' field. For example the outcome I am looking for is:

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

I can group the items together but they all remain in a single array and I can't seem to separate them from each other to generate a report

  $grouped_types = array();

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

Can anyone help me? Much appreciated!

user
  • 395
  • 2
  • 11
  • 28
  • 1
    What output/error do you get? – MEE Nov 01 '17 at 14:30
  • @PaulStrobach all the array elements are there, but just ordered by another_id, I need to have a heading of the 'Another_id' field for the report, which then has each of the names associated with that id underneath – user Nov 01 '17 at 14:32
  • I can't seem to separate the arrays by 'another_id' in order to form the report unfortunately! – user Nov 01 '17 at 14:33
  • Maybe you need to check if `$grouped_types[...]` does not exist and create it then? – MEE Nov 01 '17 at 14:34
  • If you want formatted output, you need to loop through `$grouped_types`, not just dump it. – Patrick Q Nov 01 '17 at 14:34
  • @PaulStrobach there will always be a data available at any given point so it should always exist .. I think my main confusion is how I print the report to the page, as I need to do the headings based on the other id's .. At first I was thinking could I maybe separate the array into multiple arrays, in which each array holds the items with the same 'another_id' field.. but can't seem to get that going either – user Nov 01 '17 at 14:41

3 Answers3

2

What I believe you want to do is:

$grouped_types = array();

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

Output:

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)
mega6382
  • 9,211
  • 17
  • 48
  • 69
1

First create an array to group by another id

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

Display as report

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

Use this code to add any number of fields and display them.

$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";
}
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22
  • That seems to work well! just a quick question, if I were to add another field to my array ( and also try show that on screen) how would I go about it? For example a line which originally would say "Name: Test1" would become "Name: Test1 , Age: 30" – user Nov 01 '17 at 14:49
  • Updated my answer. – Ravinder Reddy Nov 01 '17 at 15:09
1

You were actually pretty close with your grouping logic. You just needed to add a part to account for if you hadn't created a grouping for the current 'another_id' yet. Then, the output is really as simple as a couple nested loops to go through each grouping and then each item in the group.

$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

Patrick Q
  • 6,373
  • 2
  • 25
  • 34