RE: [PHP] question about multidimension array

From: Date: Mon, 19 Feb 2001 21:05:16 +0000
Subject: RE: [PHP] question about multidimension array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-40596@lists.php.net to get a copy of this message
> -----Original Message----- > From: Zenith [mailto:zenith@linuxmail.org] > Subject: [PHP] question about multidimension array > > Consider the following code: > > $ary1 = array ("one","two"); > $ary2 = array ("three","four"); > > $2d_Dimension[] = $ary1; > $2d_Dimension[] = $ary2; > > // is $2d_Dimension a 2 dimensional array? yes... the "dimension" of an array is basically how many brackets you put after the variable name... 1D = $this[]; 2D = $this[][]; 3D = $this[][][]; etc.... > // and the next question, how to get out content of the $2d_Dimension[] > array > > while ( list ( $rec_no, $ary ) = each ( $2d_Dimension ) ) > { > echo ("Record No $rec_no:"); > while ( list ( $element1, $element2 ) = each ( $ary ) ) > echo "$element1, $element2"; > } with a slight modification, that code works perfectly: (see end of email for why you probably got a parse error) <?php $ary1 = array ("one","two"); $ary2 = array ("three","four"); $twod = array($ary1, $ary2); while ( list ($rec_no,$ary) = each ($twod) ) { echo ("Record No $rec_no:"); while ( list ($key,$val) = each ($ary) ) echo "$key=$val; "; echo "<BR>\n"; } ?> > // I want to use the above code to print something like > // Record No 0:one, two > // Record No 1:three, four my code produces: Record No 0:0=one; 1=two; Record No 1:0=three; 1=four; suppress printing $key, and you'll get the output you want. > // But I only got the following > //Record No 0: > //Warning: Variable passed to each() is not an array or object in > d:/project/bizvista/testinc.php on line 27 > > What's the problem? if i use your code verbatim, i get a parse error becuase of the variable name starting with a digit, but as for the error *you* describe, when i put the loop inside of a function, and fail to declare $twod as global, i get the same error: "Warning: Variable passed to each() is not an array or object ..." make sure you declare all globals as "global $varname" in your functions... PHP is basically the reverse of almost every other language (things are local by default, global by declaration)...

« previous php.general (#40596) next »