[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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Multi-Dimensional Arrays
I am trying to create an array to hold shopping cart information. The array I am using is called "cartArray". What I want to do is to define a sub-array of cartArray with the information from one product. Then the next time a product is added it appends then new information as a second sub-array to cartArray and so forth. Following is some code I have been using to test with and so far PHP will let me use ".=" to append but when I try to call it back with "print_r" or echo array[][] only the first entry is returned. Any ideas what I am doing wrong? $brand="Brand1"; $itemnumber="456789"; $itemname="Some Item Name"; $itemqty=3; $cartArray[] .= array(0=>array($itemnumber=>"$brand", "$itemqty", "$itemname")); print_r($cartArray).""; $brand="Brand2"; $itemnumber="123456"; $itemname="Another Item Name"; $itemqty=9; array_push($cartArray, array($itemnumber=>"$brand", "$itemqty", "$itemname")); print_r($cartArray).""; echo $cartArray[0][0].""; Thank you, Jonathan Duncan -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] multi-dimensional arrays
Hi, I am learning PHP and have a simple question. I have a input string in this form: xxx xx x xx xxx xx x x xxx xx xx . . . xx xxx xx xx xx each line has 6 words of various lengths, all separated by white space. the input string can have any number of lines I want to put this into a multi-dimensional array, each line an array that is an element of an outer array. I have tried various ways to do this--I have used explode() and array_filter() and can get a single line parsed and into an array but I am having problems getting a well formed 2 dim array. What is the easiest way to do this? With all of the PHP array functions, there should be an very straight forward way to do this. Any help would be appreciated. -Andres -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi Dimensional Arrays?
I would have to say you would need a nested for loop to count your array. $j = 0 ; $k = 0 ; for ( ; $j != '' ) ; $j++ ) { //top of the col. echo "$main_menu[$j][$k]"; for ( $k = 1; $k != '' ; $k++ ) { // then everything under the col (by row) echo "$main_menu[$j][$k]"; } } - Original Message - From: "Brandon Orther" <[EMAIL PROTECTED]> To: "PHP User Group" <[EMAIL PROTECTED]> Sent: Monday, May 07, 2001 8:59 AM 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Multi Dimensional Arrays?
actually a nested foreach() is probably better. Use it in a function that calls itself recursively for all elements that are arrays and adds to the count for all that aren't. I posted a function that does this a little while ago, don't think I've got it anymore but the idea is fairly straightforward. The key to dealing with multi-dimensional arrays is in realising that they don't exist. In your example you have an array with a single element. That element is itself an array with 4 elements. Any one (or all) of these elements could be another array, hence the need for a recursive function to find the elements in a multi-dimensional array. Tim Ward Senior Systems Engineer Please refer to the following disclaimer in respect of this message: http://www.stivesdirect.com/e-mail-disclaimer.html > -Original Message- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] > Sent: 07 May 2001 18:12 > To: Brandon Orther; PHP User Group > Subject: Re: [PHP] Multi Dimensional Arrays? > > > I would have to say you would need a nested for loop to count > your array. > > $j = 0 ; > $k = 0 ; > > for ( ; $j != '' ) ; $j++ ) > { > > //top of the col. > echo "$main_menu[$j][$k]"; > > for ( $k = 1; $k != '' ; $k++ ) > { > // then everything under the col (by row) > echo "$main_menu[$j][$k]"; > } > } > > > > > - Original Message - > From: "Brandon Orther" <[EMAIL PROTECTED]> > To: "PHP User Group" <[EMAIL PROTECTED]> > Sent: Monday, May 07, 2001 8:59 AM > 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: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: > [EMAIL PROTECTED] > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
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: 4 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:[EMAIL PROTECTED]] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
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:[EMAIL PROTECTED]] Sent: Tuesday, May 15, 2001 10:30 AM To: Brandon Orther; PHP User Group Cc: [EMAIL PROTECTED] 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: 4 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:[EMAIL PROTECTED]] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Multi Dimensional Arrays?
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:[EMAIL PROTECTED]] 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:[EMAIL PROTECTED]] Sent: Tuesday, May 15, 2001 10:30 AM To: Brandon Orther; PHP User Group Cc: [EMAIL PROTECTED] 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: 4 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:[EMAIL PROTECTED]] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Multi-Dimensional Arrays
try doing this for each item: $cartArray[] = array('itemnumber' => $itemnumber, 'brand' => $brand, 'quantity' => $itemqty, 'name' => $itemname); -js Jonathan Duncan wrote: > I am trying to create an array to hold shopping cart information. The array > I am using is called "cartArray". What I want to do is to define a > sub-array of cartArray with the information from one product. Then the next > time a product is added it appends then new information as a second > sub-array to cartArray and so forth. Following is some code I have been > using to test with and so far PHP will let me use ".=" to append but when I > try to call it back with "print_r" or echo array[][] only the first entry is > returned. Any ideas what I am doing wrong? > > > $brand="Brand1"; > $itemnumber="456789"; > $itemname="Some Item Name"; > $itemqty=3; > $cartArray[] .= array(0=>array($itemnumber=>"$brand", "$itemqty", > "$itemname")); > print_r($cartArray).""; > $brand="Brand2"; > $itemnumber="123456"; > $itemname="Another Item Name"; > $itemqty=9; > array_push($cartArray, array($itemnumber=>"$brand", "$itemqty", > "$itemname")); > print_r($cartArray).""; > echo $cartArray[0][0].""; > > > Thank you, > Jonathan Duncan > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multi-Dimensional Arrays
Yeah, you rock, that works great. Not to mention you have a great name. Jonathan Duncan "Jonathan Sharp" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > try doing this for each item: > > $cartArray[] = array('itemnumber' => $itemnumber, 'brand' => $brand, > 'quantity' => $itemqty, 'name' => $itemname); > > -js > > Jonathan Duncan wrote: > > I am trying to create an array to hold shopping cart information. The array > > I am using is called "cartArray". What I want to do is to define a > > sub-array of cartArray with the information from one product. Then the next > > time a product is added it appends then new information as a second > > sub-array to cartArray and so forth. Following is some code I have been > > using to test with and so far PHP will let me use ".=" to append but when I > > try to call it back with "print_r" or echo array[][] only the first entry is > > returned. Any ideas what I am doing wrong? > > > > > > $brand="Brand1"; > > $itemnumber="456789"; > > $itemname="Some Item Name"; > > $itemqty=3; > > $cartArray[] .= array(0=>array($itemnumber=>"$brand", "$itemqty", > > "$itemname")); > > print_r($cartArray).""; > > $brand="Brand2"; > > $itemnumber="123456"; > > $itemname="Another Item Name"; > > $itemqty=9; > > array_push($cartArray, array($itemnumber=>"$brand", "$itemqty", > > "$itemname")); > > print_r($cartArray).""; > > echo $cartArray[0][0].""; > > > > > > Thank you, > > Jonathan Duncan > > > > > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multi-dimensional arrays
Nitsan Bin-Nun wrote: > $string = "xxx xx x xx xxx > xxx xx x xx xxx"; > $t = explode("\n", $string); > foreach ($t as $k => $v) $t[$k] = explode(" ", $v); > var_dump($t); > > On Mon, Apr 13, 2009 at 8:55 PM, Andres Gonzalez > wrote: > >> Hi, >> >> I am learning PHP and have a simple question. >> I have a input string in this form: >> >> xxx xx x xx xxx >> xx x x xxx xx xx >>. >>. >>. >> xx xxx xx xx xx >> >> each line has 6 words of various lengths, all separated by white space. >> the input string can have any number of lines >> >> I want to put this into a multi-dimensional array, each line an array that >> is an element of an outer array. >> >> I have tried various ways to do this--I have used explode() and >> array_filter() and can get a single line parsed and into an array but I am >> having problems getting a well formed 2 dim array. >> >> What is the easiest way to do this? With all of the PHP array functions, >> there should be an very straight forward way to do this. >> >> Any help would be appreciated. >> >> -Andres >> >> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > Well in your approach you get a bunch of empty elements where the spaces are. Here are two ways but I'm sure one preg_match_all() without the explodes and loop could do it (some guru will show us): //one way $text = 'xxx xx x xx xxx xx x x xxx xx xx xx xxx xx xx xx'; $lines = explode(PHP_EOL, $text); foreach($lines as $line) { $temp = explode(' ', $line); $result[] = array_filter($temp, 'reduce'); } function reduce($var) { return !empty($var); } print_r($result); //another way $text = 'xxx xx x xx xxx xx x x xxx xx xx xx xxx xx xx xx'; $lines = explode(PHP_EOL, $text); foreach($lines as $line) { preg_match_all('|([^\s]+)+|', $line, $matches); $result[] = $matches[1]; } print_r($result); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multi-dimensional arrays
Shawn McKenzie wrote: > Well in your approach you get a bunch of empty elements where the spaces > are. Here are two ways but I'm sure one preg_match_all() without the > explodes and loop could do it (some guru will show us): > > //one way > $text = 'xxx xx x xx xxx > xx x x xxx xx xx > xx xxx xx xx xx'; > > $lines = explode(PHP_EOL, $text); > > foreach($lines as $line) { > $temp = explode(' ', $line); > $result[] = array_filter($temp, 'reduce'); > } > function reduce($var) { > return !empty($var); > } > print_r($result); Array ( [0] => Array ( [0] => xxx [2] => [4] => xx [6] => x [8] => xx [11] => xxx ) [1] => Array ( [0] => xx [2] => x [5] => x [7] => xxx [9] => xx [11] => xx ) [2] => Array ( [0] => xx [2] => xxx [4] => xx [6] => [8] => xx [11] => xx ) ) > > //another way > $text = 'xxx xx x xx xxx > xx x x xxx xx xx > xx xxx xx xx xx'; > > $lines = explode(PHP_EOL, $text); > > foreach($lines as $line) { > preg_match_all('|([^\s]+)+|', $line, $matches); > $result[] = $matches[1]; > } > print_r($result); Array ( [0] => Array ( [0] => xxx [1] => [2] => xx [3] => x [4] => xx [5] => xxx ) [1] => Array ( [0] => xx [1] => x [2] => x [3] => xxx [4] => xx [5] => xx ) [2] => Array ( [0] => xx [1] => xxx [2] => xx [3] => [4] => xx [5] => xx ) ) There is a difference in the key numbering though if that is important. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multi-dimensional arrays
I was having the same problem. The second way was what I was looking for. Thank you so much for your help--I did not know about preg_match_all(). very coolthanks again. -Andres Shawn McKenzie wrote: Shawn McKenzie wrote: Well in your approach you get a bunch of empty elements where the spaces are. Here are two ways but I'm sure one preg_match_all() without the explodes and loop could do it (some guru will show us): //one way $text = 'xxx xx x xx xxx xx x x xxx xx xx xx xxx xx xx xx'; $lines = explode(PHP_EOL, $text); foreach($lines as $line) { $temp = explode(' ', $line); $result[] = array_filter($temp, 'reduce'); } function reduce($var) { return !empty($var); } print_r($result); Array ( [0] => Array ( [0] => xxx [2] => [4] => xx [6] => x [8] => xx [11] => xxx ) [1] => Array ( [0] => xx [2] => x [5] => x [7] => xxx [9] => xx [11] => xx ) [2] => Array ( [0] => xx [2] => xxx [4] => xx [6] => [8] => xx [11] => xx ) ) //another way $text = 'xxx xx x xx xxx xx x x xxx xx xx xx xxx xx xx xx'; $lines = explode(PHP_EOL, $text); foreach($lines as $line) { preg_match_all('|([^\s]+)+|', $line, $matches); $result[] = $matches[1]; } print_r($result); Array ( [0] => Array ( [0] => xxx [1] => [2] => xx [3] => x [4] => xx [5] => xxx ) [1] => Array ( [0] => xx [1] => x [2] => x [3] => xxx [4] => xx [5] => xx ) [2] => Array ( [0] => xx [1] => xxx [2] => xx [3] => [4] => xx [5] => xx ) ) There is a difference in the key numbering though if that is important. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multi-dimensional arrays
$string = "xxx xx x xx xxx xxx xx x xx xxx"; $t = explode("\n", $string); foreach ($t as $k => $v) $t[$k] = explode(" ", $v); var_dump($t); On Mon, Apr 13, 2009 at 8:55 PM, Andres Gonzalez wrote: > Hi, > > I am learning PHP and have a simple question. > I have a input string in this form: > > xxx xx x xx xxx > xx x x xxx xx xx >. >. >. > xx xxx xx xx xx > > each line has 6 words of various lengths, all separated by white space. > the input string can have any number of lines > > I want to put this into a multi-dimensional array, each line an array that > is an element of an outer array. > > I have tried various ways to do this--I have used explode() and > array_filter() and can get a single line parsed and into an array but I am > having problems getting a well formed 2 dim array. > > What is the easiest way to do this? With all of the PHP array functions, > there should be an very straight forward way to do this. > > Any help would be appreciated. > > -Andres > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] multi-dimensional arrays
Just wondering, there is callback version of preg_match_all() ? if so you could have done it in one line I think.. On Mon, Apr 13, 2009 at 9:52 PM, Andres Gonzalez wrote: > I was having the same problem. The second way was what I was looking for. > > Thank you so much for your help--I did not know about preg_match_all(). > > very coolthanks again. > > -Andres > > > > Shawn McKenzie wrote: > >> Shawn McKenzie wrote: >> >> >>> Well in your approach you get a bunch of empty elements where the spaces >>> are. Here are two ways but I'm sure one preg_match_all() without the >>> explodes and loop could do it (some guru will show us): >>> >>> //one way >>> $text = 'xxx xx x xx xxx >>> xx x x xxx xx xx >>> xx xxx xx xx xx'; >>> >>> $lines = explode(PHP_EOL, $text); >>> >>> foreach($lines as $line) { >>>$temp = explode(' ', $line); >>>$result[] = array_filter($temp, 'reduce'); >>> } >>> function reduce($var) { >>>return !empty($var); >>> } >>> print_r($result); >>> >>> >> Array >> ( >>[0] => Array >>( >>[0] => xxx >>[2] => >>[4] => xx >>[6] => x >>[8] => xx >>[11] => xxx >>) >> >>[1] => Array >>( >>[0] => xx >>[2] => x >>[5] => x >>[7] => xxx >>[9] => xx >>[11] => xx >>) >> >>[2] => Array >>( >>[0] => xx >>[2] => xxx >>[4] => xx >>[6] => >>[8] => xx >>[11] => xx >>) >> >> ) >> >> >>> //another way >>> $text = 'xxx xx x xx xxx >>> xx x x xxx xx xx >>> xx xxx xx xx xx'; >>> >>> $lines = explode(PHP_EOL, $text); >>> >>> foreach($lines as $line) { >>>preg_match_all('|([^\s]+)+|', $line, $matches); >>>$result[] = $matches[1]; >>> } >>> print_r($result); >>> >>> >> Array >> ( >>[0] => Array >>( >>[0] => xxx >>[1] => >>[2] => xx >>[3] => x >>[4] => xx >>[5] => xxx >>) >> >>[1] => Array >>( >>[0] => xx >>[1] => x >>[2] => x >>[3] => xxx >>[4] => xx >>[5] => xx >>) >> >>[2] => Array >>( >>[0] => xx >>[1] => xxx >>[2] => xx >>[3] => >>[4] => xx >>[5] => xx >>) >> >> ) >> >> There is a difference in the key numbering though if that is important. >> >> >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] multi-dimensional arrays
Nitsan Bin-Nun wrote: $string = "xxx xx x xx xxx xxx xx x xx xxx"; $t = explode("\n", $string); foreach ($t as $k => $v) $t[$k] = explode(" ", $v); var_dump($t); After assigning the string do $string = preg_replace('/\s+/',' ',$string); Then you should be able to do the two explodes. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] multi-dimensional arrays
Andres Gonzalez wrote: Hi, I am learning PHP and have a simple question. I have a input string in this form: xxx xx x xx xxx xx x x xxx xx xx . . . xx xxx xx xx xx each line has 6 words of various lengths, all separated by white space. the input string can have any number of lines I want to put this into a multi-dimensional array, each line an array that is an element of an outer array. I have tried various ways to do this--I have used explode() and array_filter() and can get a single line parsed and into an array but I am having problems getting a well formed 2 dim array. What is the easiest way to do this? With all of the PHP array functions, there should be an very straight forward way to do this. Any help would be appreciated. -Andres I am no guru of regex or preg_* functions. But here is what I came up with. The above outputs the following: xxx xx x xx xxx xx x x xxx xx xx xx xxx xx xx xx Array ( [0] => Array ( [0] => xxx xx x xx xxx [1] => xxx [2] => [3] => xx [4] => x [5] => xx [6] => xxx ) [1] => Array ( [0] => xx x x xxx xx xx [1] => xx [2] => x [3] => x [4] => xxx [5] => xx [6] => xx ) [2] => Array ( [0] => xx xxx xx xx xx [1] => xx [2] => xxx [3] => xx [4] => [5] => xx [6] => xx ) ) Hope this is what you are looking for. Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] multi dimensional arrays / radio buttons / mass confusion on my part
Ok.. what I have is a database of first names and last names, and some other columns that don't matter here. A form is presented to a user, lets them enter a bunch of names, attributes of the person, etc. After submission, each record is checked against the current database to see if the name currently exists, or names similar to it exist. If so, it gives the option of either: a) entering the new record or b) selecting one of the existing records Example: Enter your names, positions: Joe Schmoe, LW Random Guy, RW (submit button) After DB check for existing users: Found: () Joey Schmoe Click radio button to se this record () Create New Record No matches found for Random Guy, must be a new user. (submit button) My problem is keeping this data consistent form by form, and updating the data when an existing record is selected instead of creating a new one (the existing data needs to go into another table, so I need to grab the user id # of the existing record, etc) Any thoughts on how to approach this? After the first form, should I make a multi-dimensional array, and have the second form update/change the elements of it? I haven't used any PHP arrays in this yet, just been messing with the HTML form array stuff.. Sorry if this is confusing.. if any part needs clarification, please let me know. Thanks, Chad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] multi dimensional arrays / radio buttons / mass confusion on my part
It sounds like you're trying to do too many things at once. If you create a form to accept entries, process the entries when the form is submitted. Check each entry and only add ones that don't already exist in the database. After that, if you want to display results, start at the beginning of the array from the submitted form and pull information from the database for each entry - by this point every one of them should have an account and it's just a matter of listing their information... Sorry, rambling a little here... did that all make sense? -Original Message- From: Chad Day [mailto:[EMAIL PROTECTED]] Sent: Tuesday, July 09, 2002 1:22 PM To: [EMAIL PROTECTED] Subject: [PHP] multi dimensional arrays / radio buttons / mass confusion on my part Ok.. what I have is a database of first names and last names, and some other columns that don't matter here. A form is presented to a user, lets them enter a bunch of names, attributes of the person, etc. After submission, each record is checked against the current database to see if the name currently exists, or names similar to it exist. If so, it gives the option of either: a) entering the new record or b) selecting one of the existing records Example: Enter your names, positions: Joe Schmoe, LW Random Guy, RW (submit button) After DB check for existing users: Found: () Joey Schmoe Click radio button to se this record () Create New Record No matches found for Random Guy, must be a new user. (submit button) My problem is keeping this data consistent form by form, and updating the data when an existing record is selected instead of creating a new one (the existing data needs to go into another table, so I need to grab the user id # of the existing record, etc) Any thoughts on how to approach this? After the first form, should I make a multi-dimensional array, and have the second form update/change the elements of it? I haven't used any PHP arrays in this yet, just been messing with the HTML form array stuff.. Sorry if this is confusing.. if any part needs clarification, please let me know. Thanks, Chad -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php This message is intended for the sole use of the individual and entity to whom it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If you are not the intended addressee, nor authorized to receive for the intended addressee, you are hereby notified that you may not use, copy, disclose or distribute to anyone the message or any information contained in the message. If you have received this message in error, please immediately advise the sender by reply email and delete the message. Thank you very much. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php