[ create a new paste ] login | about

Link: http://codepad.org/7ItbYDr4    [ raw code | output | fork | 1 comment ]

PHP, pasted on Oct 31:
<?php

$yourData = array(
  0 => array(
    'team' => 1,
    'id' => 5,
    'user' => 'teamleader1',
    'Designation' => 'Team Leader',
  ),
  1 => array(
    'team' => 1,
    'id' => 6,
    'user' => 'consultant1',
    'Designation' => 'Consultant',
  ),
  2 => array(
    'team' => 1,
    'id' => 7,
    'user' => 'consultant2',
    'Designation' => 'Consultant',
  ),
  3 => array(
    'team' => 2,
    'id' => 8,
    'user' => 'consultant3',
    'Designation' => 'Consultant',
  ),
  4 => array(
    'team' => 2,
    'id' => 9,
    'user' => 'teamleader2',
    'Designation' => 'Team Leader',
  ),
);


$grouped = array();
foreach ($yourData as $item) {
  // copy item to grouped
  $grouped[$item['team']][] = $item;
}
var_dump($grouped);


Output:
array(2) {
  [1]=>
  array(3) {
    [0]=>
    array(4) {
      ["team"]=>
      int(1)
      ["id"]=>
      int(5)
      ["user"]=>
      string(11) "teamleader1"
      ["Designation"]=>
      string(11) "Team Leader"
    }
    [1]=>
    array(4) {
      ["team"]=>
      int(1)
      ["id"]=>
      int(6)
      ["user"]=>
      string(11) "consultant1"
      ["Designation"]=>
      string(10) "Consultant"
    }
    [2]=>
    array(4) {
      ["team"]=>
      int(1)
      ["id"]=>
      int(7)
      ["user"]=>
      string(11) "consultant2"
      ["Designation"]=>
      string(10) "Consultant"
    }
  }
  [2]=>
  array(2) {
    [0]=>
    array(4) {
      ["team"]=>
      int(2)
      ["id"]=>
      int(8)
      ["user"]=>
      string(11) "consultant3"
      ["Designation"]=>
      string(10) "Consultant"
    }
    [1]=>
    array(4) {
      ["team"]=>
      int(2)
      ["id"]=>
      int(9)
      ["user"]=>
      string(11) "teamleader2"
      ["Designation"]=>
      string(11) "Team Leader"
    }
  }
}


Create a new paste based on this one


Comments:
posted by ekodok on Nov 21
jjj
reply