Bluehost.com Web Hosting $6.95

extract column from multidimentional array

This is a discussion on extract column from multidimentional array within the PHP General forums, part of the PHP Programming Forums category; Hi, below you will find the code for a small script where i'm able to extract a row but ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-29-2008
It flance
 
Posts: n/a
Default 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);
?>




Reply With Quote
  #2 (permalink)  
Old 09-29-2008
Boyd, Todd M.
 
Posts: n/a
Default RE: [PHP] extract column from multidimentional array

> -----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


Reply With Quote
  #3 (permalink)  
Old 09-29-2008
Nilesh Govindrajan
 
Posts: n/a
Default Re: [PHP] extract column from multidimentional array

On Monday 29 September 2008 09:34:10 pm It flance wrote:
> 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);
> ?>


If you know the column index of the array, you can do it easily.

$row = $arr[0]; // gives you -array('00', '01', '02', '03')

Now use this to get the first column -

<?

for( $i=0; $i <= count($arr); $i++ ) {

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

}

print_r($col);

?>
Reply With Quote
  #4 (permalink)  
Old 09-29-2008
Philip Thompson
 
Posts: n/a
Default Re: [PHP] extract column from multidimentional array

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

>> -----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


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


Reply With Quote
  #5 (permalink)  
Old 09-29-2008
Nilesh Govindrajan
 
Posts: n/a
Default Re: [PHP] extract column from multidimentional array

On Monday 29 September 2008 10:15:29 pm Philip Thompson wrote:
> On Sep 29, 2008, at 11:08 AM, Boyd, Todd M. wrote:
> >> -----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

>
> 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


I think he wants to get the whole column.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)

iEYEABECAAYFAkjhCNkACgkQsqLfUm3DlgsYaACgnqzCDRId8u lliM1AjJFV5huT
MIQAn26VeOQpy9IkQ+qgFG3Yk1OVSWqG
=lGJR
-----END PGP SIGNATURE-----

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 05:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0