Re: extract column from multidimentional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sep 29, 2008, at 11:08 AM, Boyd, Todd M. wrote:

-----Original Message-----
From: It flance [mailto:itmaqurfe@xxxxxxxxx]
Sent: Monday, September 29, 2008 11:04 AM
To: php-general@xxxxxxxxxxxxx
Subject:  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

Or in his original code....

$col = $arr[0][0];

to get the first (element of the first sub array). Think of your array as this....

$arr = array(
        0 => array(0=>'00', 1=>'01', 2=>'02', 3=>'03'),
        1 => array(0=>'10', 1=>'11', 2=>'12', 3=>'13'),
        2 => array(0=>'20', 1=>'21', 2=>'22', 3=>'23'),
    );

Notice the specific keys? That should help in determining which keys you're looking for.

I'm probably totally off by what you're asking, but Todd's response hopefully helped you as well. =D

~Philip



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux