Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "J.O. Aho" Newsgroups: comp.lang.php Subject: Re: Fast/Easy way to extract a column from multi-dimensional array? Date: Fri, 27 Jan 2012 21:50:04 +0100 Lines: 73 Message-ID: <9oggvsF8tcU1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net NgsEouYOr1QekcuTuhld6wqtneHT3tusJD1MR2qtn9Wz/yN4pE Cancel-Lock: sha1:ygOp3YOyh1a469HN+kzUt+QgvkY= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20111226 Firefox/9.0.1 SeaMonkey/2.6.1 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.php:4644 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 1, 'name' =>'A'), array('RecordID' => 2, 'name' =>'B'), array('RecordID' => 3, 'name' =>'C'), array('RecordID' => 4, 'name' =>'D'), array('RecordID' => 5, 'name' =>'E') ); // 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). -- //Aho