Re: Fast/Easy way to extract a column from multi-dimensional array? [message #176848 is a reply to message #176838] |
Sat, 28 January 2012 20:28 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
Jeff North wrote:
> John Drako wrote:
>> | I'm looking for a way to get all the values from the column of a two
>> | dimensional array.
>> |
>> | For example, I have a query on a mysql database that returns 10 rows
>> | from the database, I would like to quickly (read low cpu load) extract
>> | all the IDs from all the rows returned to reuse in another query.
>> |
>> | So let's say I use:
>> |
>> | $arr = mysqli_fetch_all($result, MYSQLI_ASSOC);
>> |
>> | and $arr looks like:
>> |
>> | RecordID, name, last name, title
>> | RecordID, name, last name, title
>> | RecordID, name, last name, title
>> | RecordID, name, last name, title
>> | RecordID, name, last name, title
>> | RecordID, name, last name, title
>> | etc...
>> |
>> | I need to get all the 'RecordID' from the results to reuse in other
>> | queries.
>> |
>> | Is there a built in PHP function (compiled code) to accomplish this?
>> | I've read the definitions of all the functions related to arrays and if
>> | it exists, I didn't find it.
>> |
>> | The site is very busy and CPU cycles count. I'm trying to avoid left
>> | joining three gigantic tables.
>
> Let the database do it for you - look at group_concat().
GROUP_CONCAT() could only return a string of the concatenated non-NULL
RecordIDs. That is probably not what the OP is looking for ("reuse in
other queries").
<http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html>
I fail to see how you could suggest such error-prone and expensive an
approach (first concatenation in MySQL, then splitting in PHP) when *the*
solution has been presented already.
Please skip the attribution novel next time.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
|
|
|