-1

I have a database table (QUEUE) like this:

queueId phoneNumber 
1       340 000
1       340 111    1
1       340 222
2       332 000
2       332 111
3       421 000
3       421 111
3       421 222

I use this query:

SELECT * FROM queue ORDER BY queueId

and this php code:

while ($rowQueue = mysql_fetch_array($resultQueryQueue, MYSQL_ASSOC)) {
            $queue[] = array(
                'queueId' => $rowQueue['queueId'],
                'phoneNumber' => $rowQueue['phoneNumber']
      ); 
}

result is a array with 8 arrays (beacuse record are 8).

I would like to get an array that contains arrays as there are so many keys. In my example I would like to get 3 arrays, the first key 1, the second and the third with key 2 and key 3.

How can I make PHP? Is there any function that can help me?

user3065205
  • 147
  • 1
  • 4
  • What do you want exactly? If you don't want all of them in one array, then don't put all of them in one array. – Tim Seguine Nov 17 '14 at 17:33
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). You will also want to [Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 17 '14 at 17:34
  • try this line inside while loop.." array_push($queue[$rowQueue['queueId']], $rowQueue['phoneNumber'])" – Asik Nov 17 '14 at 17:38

1 Answers1

0

I think of two way to do that (off course there will be few other). One you can do with mysql query IF "queryId" is a foreign key you can get all the phoneNumber associated with specific queryId

SECOND: i am Guessing you will get a result from query is like this

$arr = array(
          array(
                  'queryid' => 1,
                  'phonenumber'=>350000
               ),
          array(
                  'queryid' => 1,
                  'phonenumber'=>350000
               ),
          array(
                  'queryid' => 2,
                  'phonenumber'=>340001
                ),
          array(
                  'queryid' => 2,
                  'phonenumber'=>340002
               )
     );

You can sort this by

$ret = array();
foreach ($arr as $k => $v)
{

      $ret [$v['queryid']][]=$v['phonenumber'];
}
var_dump($ret);

key of this array will be 'queryid' and will have array of phonenumber related to that key And also consider removing space from phone number. Let me know if it worked. thanks

Harry
  • 44
  • 6