RE: [PHP] extract column from multidimentional array

From: Date: Mon, 29 Sep 2008 16:08:43 +0000
Subject: RE: [PHP] extract column from multidimentional array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-281108@lists.php.net to get a copy of this message
> -----Original Message----- > From: It flance [mailto:itmaqurfe@yahoo.com] > Sent: Monday, September 29, 2008 11:04 AM > To: php-general@lists.php.net > Subject: [PHP] extract column from multidimentional array > > Hi, > > below you will find the code for a small script where i'm able to > extract a row but not a column. > > So the question is how can I do that. > > Here is the code: > > <?php > > $arr = array( > array('00', '01', '02', '03'), > array('10', '11', '12', '13'), > array('20', '21', '22', '23'), > ); > > $row = $arr[0]; > $col = $arr[][0]; > print_r($row); > print "\n"; > print_r($col); > ?> Being that PHP is row-major (like most programming languages), that won't work. Loop through the array and build a new one out of just the 1st column from every row (or whatever column you're looking for): <?php $colarr = array(); for($a = 0; $a < count($arr); $a++) { $colarr[$a] = $arr[$a][0] } print_r($colarr) ?> HTH, Todd Boyd Web Programmer

« previous php.general (#281108) next »