Re: Displaying full array contents

From: Date: Fri, 27 Sep 2002 00:56:35 +0000
Subject: Re: Displaying full array contents
Groups: php.general 
Request: Send a blank email to php-general+get-117800@lists.php.net to get a copy of this message
You could always use a for loop enbeded in a while loop to walk through a linked list. <html> <body> <?php $Arr1[1] = "Apple"; $Arr1[2] = "Banana"; $Arr1[3] = &$Arr2; $Arr2[1] = "Carrot"; $Arr2[2] = &$Arr3; $Arr3[1] = "Orange"; $Arr3[2] = "Peach"; $Arr3[3] = ""; //Anything that is used to define the end of the list. print_r($Arr1); //used a pre-existing function. $handle = &$Arr1; while ($handle != ""){ for( $i=1; $i < sizeof($handle) ; $i++){ echo " "; echo $handle[$i]; } $handle = $handle[sizeof($handle)]; //last value in the array contains the address of the next array. } ?> </body> </html> You could expand the list out as long as you have memory and this function would walk through it until it found the end. But the true questions is why would you want to when you can already dynamically change the size of arrays in php. ----------------------------------------------------------- IRS: Income Removal Service ----------------------------------------------------------- This Quote has been brought to you in part by the Letter C. For C is for cookie. ----- Original Message ----- From: "debbie_dyer" <debbie_dyer@surfanytime.co.uk> To: <php-general@lists.php.net> Sent: Thursday, September 26, 2002 1:23 PM Subject: Re: [PHP] Displaying full array contents Easier yes and ok for debug but it doesnt look very nice on a web page does it nor does it help if you want to do something with the array elements. Debbie ----- Original Message ----- From: "Martin W Jørgensen" <powerblade@mail.dk> To: <php-general@lists.php.net> Sent: Thursday, September 26, 2002 9:17 PM Subject: Re: [PHP] Displaying full array contents > print_r($array); simply print out the entire array.. > It cant be easier. > > "Debbie_dyer" <debbie_dyer@surfanytime.co.uk> wrote in message > news:01bf01c26598$1d84ec00$0100a8c0@homepc... > > Use a static variable in the function? A static var retains its value > > between function calls > > > > function printArray($arr) { > > static $depth = 0; > > for ($i =0; $i < count($arr); $i++) { > > if (!is_array($arr[$i])) { > > echo "$depth $arr[$i]"; > > } > > else { > > $depth++; > > printArray($arr[$i]); > > $depth--; > > } > > } > > } > > > > $arr = array("Orange", "Peach", "Apple"); > > $arr2 = array("Banana", $arr, "Pear"); > > $arr3 = array($arr, $arr2); > > > > printArray($arr3); > > > > Debbie > > > > ----- Original Message ----- > > From: "Brad Harriger" <bradharriger@clarionhospital.org> > > To: <php-general@lists.php.net> > > Sent: Thursday, September 26, 2002 8:46 PM > > Subject: Re: [PHP] Displaying full array contents > > > > > > > Debbie, > > > > > > Yes. I could use recursion, but what's really hanging me up is keeping > > > track of how deep into an array I am. It should be fairly simple, but I > > > seem to be having a brain freeze. > > > > > > Brad > > > > > > > > > > > > Debbie_dyer wrote: > > > > > > > You could use recursion example:- > > > > > > > > function printArray($arr) { > > > > for ($i =0; $i < count($arr); $i++) { > > > > if (!is_array($arr[$i])) { > > > > echo $arr[$i]; > > > > } > > > > else { > > > > printArray($arr[$i]); > > > > } > > > > } > > > > } > > > > > > > > $arr = array("Orange", "Peach", "Apple"); > > > > $arr2 = array("Banana", $arr, "Pear"); > > > > $arr3 = array($arr, $arr2); > > > > > > > > printArray($arr3); > > > > > > > > Debbie > > > > > > > > ----- Original Message ----- > > > > From: "Brad Harriger" <bradharriger@clarionhospital.org> > > > > To: <php-general@lists.php.net> > > > > Sent: Thursday, September 26, 2002 6:50 PM > > > > Subject: [PHP] Displaying full array contents > > > > > > > > > > > > > > > >>I'm trying to write a function that will display the full contents of > an > > > >>array. If one of the keys contains a value that is an array, the full > > > >>array (all indices) should be shown. > > > >> > > > >>As an example, given the following definitions: > > > >> > > > >>$Arr1[1] = "Apple"; > > > >>$Arr1[2] = "Banana"; > > > >>$Arr1[3] = $Arr2[]; > > > >>$Arr2[1] = "Carrot"; > > > >>$Arr2[2] = $Arr3[]; > > > >>$Arr3[1] = "Orange"; > > > >>$Arr3[2] = "Peach"; > > > >> > > > >> > > > >>the output should be: > > > >> > > > >>Arr1:1:Apple > > > >>Arr1:2:Banana > > > >>Arr1:3:Arr2[] > > > >>Arr1:3:Arr2:1:Carrot > > > >>Arr1:3:Arr2:2:Arr3[] > > > >>Arr1:3:Arr2:2:Arr3:1:Orange > > > >>Arr1:3:Arr2:2:Arr3:2:Peach > > > >> > > > >>The closest I've come is: > > > >> > > > >> while (current($myArr)) > > > >> { > > > >> if(is_array(current($myArr))) > > > >> { > > > >> $arrKey = key(current($myArr)); > > > >> echo "Array "; > > > >> echo "&nbsp=&nbsp"; > > > >> $baseArray = key($myArr); > > > >> echo key($myArr); > > > >> echo "<BR>\n"; > > > >> walkArray(current($myArr)); > > > >> } > > > >> else > > > >> { > > > >> $arrKey = key($myArr); > > > >> if ($baseArray != "") > > > >> { > > > >> echo $baseArray; > > > >> echo ":"; > > > >> } > > > >> echo $arrKey; > > > >> echo "&nbsp=&nbsp"; > > > >> echo current($myArr); > > > >> echo "<BR>\n"; > > > >> } > > > >> next($myArr); > > > >> } > > > >> > > > >>This code only echoes one dimension of a multi-dimension array. I > can't > > > >>find a way to reliably store more than that. Any suggestions? > > > >> > > > >>Thanks in advance, > > > >> > > > >>Brad > > > >> > > > >> > > > >>-- > > > >>PHP General Mailing List (http://www.php.net/) > > > >>To unsubscribe, visit: > > > >>http://www.php.net/unsub.php > > > >> > > > >> > > > > > > > > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#117800) next »