RE: [PHP] Multi Dimensional Arrays?

From: Date: Tue, 15 May 2001 15:13:26 +0000
Subject: RE: [PHP] Multi Dimensional Arrays?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-52872@lists.php.net to get a copy of this message
Check out the count() function in the manual http://www.php.net/count . There are some user contributed functions towards the bottom of the page that claim to count all non-null values in a multi-dimensional array. Robert Zwink -----Original Message----- From: Romulo Roberto Pereira [mailto:rpereira@vox2vox.com] Sent: Tuesday, May 15, 2001 10:55 AM To: Robert V. Zwink; PHP User Group Subject: RE: [PHP] Multi Dimensional Arrays? Anyone knows how to determine how many levels has an array? TIA Rom -----Original Message----- From: Robert V. Zwink [mailto:rzwink@microcenter.com] Sent: Tuesday, May 15, 2001 10:30 AM To: Brandon Orther; PHP User Group Cc: tim.ward@stivesdirect.com Subject: RE: [PHP] Multi Dimensional Arrays? You are on the right track but doing something wrong: You are correctly assigning a string to an array: //Main Menu Array $main_menu[0] = "Menu 1"; //Represents the name of the menu But then you are taking that string one character at a time and replacing it with invalid data: //Menu 1 Array $main_menu[0][0] = "Test 1"; $main_menu[0][1] = "Test 2"; $main_menu[0][2] = "Test 3"; $main_menu[0][3] = "Test 4"; The result is echo $main_menu[0] prints: TTTT4 This is doing exactly what you are asking it to do. Create an array then one character at a time replace it with with the first character in the provided string. You would be better of doing this: $main_menu[0][] = "Title of the Menu"; $main_menu[0][] = "Test 1"; $main_menu[0][] = "Test 2"; $main_menu[0][] = "Test 3"; $main_menu[0][] = "Test 4"; That way element [0] will always be the title and all following values will be elements in that menu. If you do the above, then type: echo count($mail_menu[0]); It will return 5. This whole thing about recursive funcions to count multiple dimensional arrays that don't really exist is incorrect. The count() function will work fine in your example, just build your multi-dimensional array correctly. If you need to count the all the elements in a multi-dimensional array then you will need to write a function that is recursive, but you shouldn't need to iterate through each element incrementing a counter to do so. Just use count(). It works fine on multi-dimensional arrays. Robert Zwink -----Original Message----- From: Brandon Orther [mailto:orther@webintellects.com] Sent: Monday, May 07, 2001 12:00 PM To: PHP User Group Subject: [PHP] Multi Dimensional Arrays? Hello, I am working on a menu script that I wanted to use multi-dimensional arrays for. Here is the conf file I have: //Main Menu Array $main_menu[0] = "Menu 1"; //Represents the name of the menu //Menu 1 Array $main_menu[0][0] = "Test 1"; $main_menu[0][1] = "Test 2"; $main_menu[0][2] = "Test 3"; $main_menu[0][3] = "Test 4"; When I try to us count like: $count = count($main_menu[0]); I only get 1. My question is how do I count a multi dimensional array. Thanks for the help, Brandon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#52872) next »