tags:

views:

30

answers:

1

I'm working with a mysql group_concat function and I'm having some problems. I've just noticed that the array value really isn't a value but rather a part of my result which is not correct. I need to make the string the value so that I can reference it.

Here is the portion of the SQL that I have:

GROUP_CONCAT(t3.location, t2.content SEPARATOR ',')

This produces:

Array
(
    [0] => name   my value
    [1] => name   my value
    [2] => dept   my value

.......

As you can see, "name" as well as "dept" are pert of the value "my value". What I want is:

Array
(
    [name] => [0]   my value
    [name] => [1]   my value
    [dept] => [0]   my value

.......
A: 

This works, assuming it's the first section of whitespace that separates your desired array keys from the rest of the value.

$sample = array( 'name  my value', 'name  my value', 'dept  my value' );

$result = array();
foreach ( $sample as $item )
{
  if ( preg_match( "/(\S+)\s+(.*)/", $item, $matches ) )
  {
    $result[$matches[1]][] = $matches[2];
  }
}
Peter Bailey