Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #4647
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Fast/Easy way to extract a column from multi-dimensional array? |
| Date | 2012-01-27 22:49 +0100 |
| Message-ID | <9ogkfaF3kkU1@mid.uni-berlin.de> (permalink) |
| References | <jfv06v01hoa@news6.newsguy.com> <9oggvsF8tcU1@mid.individual.net> |
Am 27.01.2012 21:50, schrieb J.O. Aho:
> 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.
>
> There is no function which will do it, but there are ways to make it quite simple
>
> <?php
>
> // Assume we got the values out of the database
> $arr = array(
> array('RecordID' => 1, 'name' =>'A'),
> array('RecordID' => 2, 'name' =>'B'),
> array('RecordID' => 3, 'name' =>'C'),
> array('RecordID' => 4, 'name' =>'D'),
> array('RecordID' => 5, 'name' =>'E')
> );
>
Yes, you get an array of arrays.
> // this is a function which will only get the RecordID
> function getRecordID($item, $key, $newarray) {
> if($key == 'RecordID')
> $newarray[]= $item;
> }
>
> // This is the array where to store the IDs
> $arr_of_id = array();
>
> // here we do fetch all the IDs
> array_walk_recursive($arr, 'getRecordID',&$arr_of_id);
>
> // Here you see we got them all
> var_dump($arr_of_id);
> ?>
>
>
>> The site is very busy and CPU cycles count. I'm trying to avoid left
>> joining three gigantic tables.
>
> I think you will need less CPU with the left join, I suggest you look at "having" if
> you want to use a smaller range in the joins.
>
> Don't forget, if you have something you fetch often and which seldom changes, it can
> be wise to cache those somewhere (either use MySQLs built in cache or memcached or
> similar).
>
Yes, databases are made for joining. Think twice before doing it in your script.
I am answering to your post because you write exactly what I thought when reading the
question.
/Str.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fast/Easy way to extract a column from multi-dimensional array? John Drako <jbravo556@gmail.removethis.com> - 2012-01-27 15:08 -0500
Re: Fast/Easy way to extract a column from multi-dimensional array? "J.O. Aho" <user@example.net> - 2012-01-27 21:50 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-27 22:49 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-01-27 21:52 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-27 22:42 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-27 22:55 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas Mlynarczyk <thomas@mlynarczyk-webdesign.de> - 2012-01-27 23:49 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? Jeff North <jnorthau@yahoo.com.au> - 2012-01-28 13:52 +1100
Re: Fast/Easy way to extract a column from multi-dimensional array? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-28 21:28 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? Arno Welzel <usenet@arnowelzel.de> - 2012-01-28 09:49 +0100
Re: Fast/Easy way to extract a column from multi-dimensional array? John Drako <jbravo556@gmail.removethis.com> - 2012-01-29 13:54 -0500
csiph-web