Create PHP array from MySQL's GROUP_CONCAT

Alex G

I get data from MySQL query by using GROUP_CONCAT:

GROUP_CONCAT('id => ',assignments.userid,', assigned => ',assignments.assigned SEPARATOR ';') as assigneeids

And trying to convert it to PHP array.

$assignees = explode(';', $ticket['assigneeids']);
foreach($assignees as $assignee) {
    echo "$assignee\n"; // $assignee['id'] outputs 'i'
    echo gettype($assignee) . '\n';
}

But unfortunately $assignee becomes a string instead of array. Output:

id => 1001, assigned => 1419648601
string
id => 1002, assigned => 1419649207
string

What am I doing wrong?

Darren

You're concatenating it into a string, not an array. Would you not be better off doing something like this?

GROUP_CONCAT(assignments.userid,'_',assignments.assigned SEPARATOR ';') as assigneeids

Once fetched, you'll need to do some explode()'ing magic

$assignees = explode(';', $ticket['assigneeids']);
foreach($assignees as $assignee) {
    list($id, $assigned) = explode("_", $assignee);
    echo "$id\n";
    echo gettype($assigned) . '\n';
}

Example/Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create PHP array from MySQL's GROUP_CONCAT

From Dev

MySQL GROUP_CONCAT from subquery

From Dev

Search running spatially with PHP, MYSQL, GROUP_CONCAT and JOIN

From Dev

PHP MySQL Treat GROUP_CONCAT results as separate items

From Dev

Spark SQL replacement for MySQL's GROUP_CONCAT aggregate function

From Dev

PHP : create a specific array from MySQL database

From Dev

How to create PHP array from MySQL table

From Dev

How get max value from result of group_concat mysql

From Dev

MySQL - GROUP_CONCAT with two columns from different tables

From Dev

How get max value from result of group_concat mysql

From Dev

MySQL Group_Concat Not In

From Dev

MySQL's Group_Concat function miss the nulls. How can group the rows including NULLs.

From Dev

If condition with group_concat in mysql

From Dev

Mysql with GROUP_CONCAT in subselect

From Dev

Mysql GROUP_CONCAT and IN query

From Dev

MySQL group_concat with join

From Dev

MySQL group_concat and count

From Dev

mySQL GROUP_CONCAT - Query

From Dev

MySQL: Nested GROUP_CONCAT

From Dev

Inversing group_concat in Mysql

From Dev

MySQL group_concat problems

From Dev

PHP explode group_concat

From Dev

PHP explode group_concat

From Dev

group_concat is not working in php

From Dev

Create An Associative Array In PHP From MySQL With Duplicate Values

From Dev

Create An Associative Array In PHP From MySQL With Duplicate Values

From Dev

Create a new column in an array of data fetched from mysql table in php

From Dev

How to create and fill php array from Mysql database

From Dev

PHP/MySQL - Trying to create an array from while loop