0

please I have table 'data'

status  name
------------
b       X
a       E
a       B
b       I

and I would like to get array like this:

array(
  'a'=> array('B', 'E'),
  'b'=> array('I', 'X')
)

How should I write query to get this? As you can see, everything should be ordered alphabetically.

peter
  • 4,289
  • 12
  • 44
  • 67
  • possible duplicate of [How can I simulate an array variable in MySQL?](http://stackoverflow.com/questions/12176709/how-can-i-simulate-an-array-variable-in-mysql) – Air Nov 15 '13 at 16:36
  • I assume you want that array in PHP? What have you tried so far? – gen_Eric Nov 15 '13 at 16:51
  • yes, in php. I can do build of array in php for every mysql record (do find if exist in array and if not then add it) but when I have 30 000+ records I hoped there is easier solution. Probably the best should be to do it directly in mysql query. But I have no Idea how to associate and group these fields- – peter Nov 15 '13 at 17:17

1 Answers1

0

well, I made this nice function to sort my stuff as needed. Hope it helps someone.

function sort_status_name($data) {
  $pole = array();

  foreach ($data as $item)
  {
    if (array_key_exists($item->status, $pole)) {
        if (!in_array($item->name, $pole[$item->status])) {
            array_push($pole[$item->status], $item->name); 
        }      
    }
    else
    {
        $pole[$item->status] = array();
        array_push($pole[$item->status], $item->name); 
    }
  }

  return $pole;

}
peter
  • 4,289
  • 12
  • 44
  • 67