[PHP] Multidimensional Arrays
I'm trying to submit multiple values to a variable within an option tag by using a value such as value=this,that There are multiple select statements on this form each counting up from 1 using $count and $listings as such. ? $count = 1; $listings = 3 while ($count = $listings) {? select name=listings[? echo $count; ? value=1 option value=this,thatThis and That option value=that,thisThat and This ? $count++; } ? On the recieving side I'm parsing many instances of these using the following method $count = 1; $listings = 3; while ($count = $listings) { $listing['$count'] = $_POST['listing[$count]']; } This example works fine if I need to get the full value (this,that) or (that,this). What I'd like to do is break this into a multidimensional array so I have approximately the following depending on what they choose for each example. $listing[1][0] = this $listing[1][1] = that $listing[2][0] = that $listing[2][0] = this Hopefully this example is clear enough that someone will understand what I'm doing. I tried the following with no luck. $listing['count'] = $_POST['listing[count]']; $listing['$count'] = explode(,, $listing['$count']); Any takers? Thanks. Ed -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multidimensional Arrays
Ed Curtis mailto:[EMAIL PROTECTED] on Monday, October 20, 2003 11:39 AM said: What I'd like to do is break this into a multidimensional array so I have approximately the following depending on what they choose for each example. [snip] I tried the following with no luck. $listing['count'] = $_POST['listing[count]']; $listing['$count'] = explode(,, $listing['$count']); Dis: function returnArray($input) { $arrCnt = count($input); $iCnt = 0; while($iCnt $arrCnt) { $listing[$iCnt] = explode(,, $input[$iCnt]); } return $listing; } Untested, but looks ok. Chris. -- Don't like reformatting your Outlook replies? Now there's relief! http://home.in.tum.de/~jain/software/outlook-quotefix/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Multidimensional arrays
Hi Can anybody help me grasp multidimensional arrays please? Basically, I am retrieving all records from one table (two fields), and want to return them in an array. I am converting code from ASP to PHP, and in the former I did something like this: select id, name from customer redim custarray(recordcount,2) i = 0 while not eof custarray(i,0) = id; custarray(i,1) = name; i = i+1; movenext wend ... but all my efforts to store them in the same kind of way in PHP have proved fruitless. Any advice would be much appreciated. Many thanks Gary -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays
snip select id, name from customer redim custarray(recordcount,2) i = 0 while not eof custarray(i,0) = id; custarray(i,1) = name; i = i+1; movenext wend /snip Not sure what kind of db you are using but I put this together using postgresql using the manual. So this is untested. $conn = pg_connect(host=localhost dbname=whatever); $result = pg_exec($conn, select id, name from customer); $i = 0; while ($row = pg_fetch_array($result)) { $custarray[$i][0] = $row[id]; $custarray[$i][1] = $row[name]; $i++; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays
Hi Many thanks for your time Matt. I tried that method amongst a few others, and couldn't seem to get it right. What I have eventually come across is the function 'array-push' (bizarre!) to add a new array line for each record (after it's been initially defined). It seems to work for me, and hopefully it is the correct, standard way to do such a task. $strCusts = array(array('id','name')); /* setting up empty array for cust id and name */ if (mysql_num_rows($resCusts) 0) { $intCusts = mysql_num_rows($resCusts); /* store the number of records retrieved */ while ($oRsCusts = mysql_fetch_array($resCusts)) { array_push($strCusts,array($oRsCusts[id],$oRsCusts[name])); /* add new array row */ } } Many thanks once again. Gary Matt Matijevich [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... snip select id, name from customer redim custarray(recordcount,2) i = 0 while not eof custarray(i,0) = id; custarray(i,1) = name; i = i+1; movenext wend /snip Not sure what kind of db you are using but I put this together using postgresql using the manual. So this is untested. $conn = pg_connect(host=localhost dbname=whatever); $result = pg_exec($conn, select id, name from customer); $i = 0; while ($row = pg_fetch_array($result)) { $custarray[$i][0] = $row[id]; $custarray[$i][1] = $row[name]; $i++; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays
Gary Broughton [EMAIL PROTECTED] wrote: Hi Many thanks for your time Matt. I tried that method amongst a few others, and couldn't seem to get it right. What I have eventually come across is the function 'array-push' (bizarre!) to add a new array line for each record (after it's been initially defined). It seems to work for me, and hopefully it is the correct, standard way to do such a task. $strCusts = array(array('id','name')); /* setting up empty array for cust id and name */ if (mysql_num_rows($resCusts) 0) { $intCusts = mysql_num_rows($resCusts); /* store the number of records retrieved */ while ($oRsCusts = mysql_fetch_array($resCusts)) { array_push($strCusts,array($oRsCusts[id],$oRsCusts[name])); /* add new array row */ thats to complicated just use: $strCust[] = $osRsCusts; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Multidimensional arrays
Here's a way of doing it: -- $query = SELECT id, name FROM customer; $result = mysql_query($query); while ($oRsCusts = mysql_fetch_array($result)){ $customers_array[] = array( 'id' = $oRsCusts['id'], 'name' = $oRsCusts['name'] ); } -- You now have your db results store in array $customers_array Hope this help. -Original Message- From: Gary Broughton [mailto:[EMAIL PROTECTED] Sent: Friday, July 18, 2003 8:49 AM To: [EMAIL PROTECTED] Subject: Re: [PHP] Multidimensional arrays Hi Many thanks for your time Matt. I tried that method amongst a few others, and couldn't seem to get it right. What I have eventually come across is the function 'array-push' (bizarre!) to add a new array line for each record (after it's been initially defined). It seems to work for me, and hopefully it is the correct, standard way to do such a task. $strCusts = array(array('id','name')); /* setting up empty array for cust id and name */ if (mysql_num_rows($resCusts) 0) { $intCusts = mysql_num_rows($resCusts); /* store the number of records retrieved */ while ($oRsCusts = mysql_fetch_array($resCusts)) { array_push($strCusts,array($oRsCusts[id],$oRsCusts[name])); /* add new array row */ } } Many thanks once again. Gary Matt Matijevich [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED]... snip select id, name from customer redim custarray(recordcount,2) i = 0 while not eof custarray(i,0) = id; custarray(i,1) = name; i = i+1; movenext wend /snip Not sure what kind of db you are using but I put this together using postgresql using the manual. So this is untested. $conn = pg_connect(host=localhost dbname=whatever); $result = pg_exec($conn, select id, name from customer); $i = 0; while ($row = pg_fetch_array($result)) { $custarray[$i][0] = $row[id]; $custarray[$i][1] = $row[name]; $i++; } -- 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] Multidimensional arrays (more and more...)
Wow. This goes way beyond simply printing multidimensional arrays. Here's some sample data: $issue[][number] = number; $issue[][headline] = headling; $issue[][writers] = writers; $issue[][list] = list; $issue[][senttosubscribers] = 0; $issue[][month] = 05; $issue[][year] = 2003; $issue[][description] = description; What I need to do now is count(), sort() by number, and loop through this array. I read that PHP is screwy when counting and sorting multidimensional arrays, but they *have* to have come up with a method, right? __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays (more and more...)
if [$number] is unique, use it as the key: // $issue[][number] = number; -- removed $issue[$number][headline] = headling; $issue[$number][writers] = writers; $issue[$number][list] = list; $issue[$number][senttosubscribers] = 0; $issue[$number][month] = 05; $issue[$number][year] = 2003; $issue[$number][description] = description; then sorting by the the number will be simple (ksort) Mako Shark wrote: Wow. This goes way beyond simply printing multidimensional arrays. Here's some sample data: $issue[][number] = number; $issue[][headline] = headling; $issue[][writers] = writers; $issue[][list] = list; $issue[][senttosubscribers] = 0; $issue[][month] = 05; $issue[][year] = 2003; $issue[][description] = description; What I need to do now is count(), sort() by number, and loop through this array. I read that PHP is screwy when counting and sorting multidimensional arrays, but they *have* to have come up with a method, right? __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays (more and more...)
if [$number] is unique, use it as the key: Yeah, I'd thought of that except there's no guarantee that it will be unique. Plus, it makes it harder to go through with a simple loop because $number really isn't guaranteed to be numeric (okay, so I have to change the name). Plus, it may not even start from 0, For instance, it doesn't have to be from 1-10, it could be from 75-85. __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays (more and more...)
You don't need sequential numbers as your key. Take this example: /*** define some values ***/ $fruits['apple']['color']=$color; $fruits['apple']['diameter']=$x; $fruits['orange']['color']=$color; $fruits['orange']['diameter']=$x; $fruits['grape']['color']=$color; $fruits['grape']['diameter']=$x; /*** Grab the keys ***/ $fruit_types = array_keys($fruits); $count = 0; while ($count count($fruit_types)) { $fruit = $fruit_types[$count]; $color = $fruits[$fruit]['color']; $diameter = $fruits[$fruit]['diameter']; } Matt - Original Message - From: Mako Shark [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Thursday, November 28, 2002 3:45 PM Subject: Re: [PHP] Multidimensional arrays (more and more...) if [$number] is unique, use it as the key: Yeah, I'd thought of that except there's no guarantee that it will be unique. Plus, it makes it harder to go through with a simple loop because $number really isn't guaranteed to be numeric (okay, so I have to change the name). Plus, it may not even start from 0, For instance, it doesn't have to be from 1-10, it could be from 75-85. __ Do you Yahoo!? Yahoo! Mail Plus - Powerful. Affordable. Sign up now. http://mailplus.yahoo.com -- 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] Multidimensional arrays
Hi, Given the array $array initialized by: $query = SELECT id,name FROM MYTABLE; $result = mysql_query($query); $nrows = mysql_num_rows($result); for ($i=0; $i$nrows; $i++) { $arr = mysql_fetch_array($result); $array[$i]['id'] = $arr['id']; $array[$i]['name'] = $arr['name']; } I know I can have a similar 'for' loop to step through the array, from 0 to count($array) But I would like to do something more elegant, with a foreach loop. But I have been unsuccessful as a foreach seems appropriate only with a single dimensional array. Can someone suggest a more elegant solution? Thanks, Rich P.S. yes, I am fairly new to PHP, less than 6 months. Probably my array loading method could be improved also!
RE: [PHP] Multidimensional arrays
[snip] $query = SELECT id,name FROM MYTABLE; $result = mysql_query($query); $nrows = mysql_num_rows($result); for ($i=0; $i$nrows; $i++) { $arr = mysql_fetch_array($result); $array[$i]['id'] = $arr['id']; $array[$i]['name'] = $arr['name']; } I know I can have a similar 'for' loop to step through the array, from 0 to count($array) But I would like to do something more elegant, with a foreach loop. But I have been unsuccessful as a foreach seems appropriate only with a single dimensional array. Can someone suggest a more elegant solution? Thanks, Rich [/snip] That is kind of an ambiguos question there Rich. What needs to be more elegant, the method? the output? You could use a foreach key/value pairing if your values are such. See http://www.php.net/manual/en/control-structures.foreach.php HTH! Jay If you must choose between two evils, pick the one youve never tried before! *** * Texas PHP Developers Conf Spring 2003 * * T Bar M Resort Conference Center * * New Braunfels, Texas* * San Antonio Area PHP Developers Group * * Interested? Contact [EMAIL PROTECTED] * *** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Multidimensional arrays
foreach($array as $i=$values) { echo $values['id'],$values['name'] } On Mon, 19 Aug 2002, Richard Fox wrote: Hi, Given the array $array initialized by: $query = SELECT id,name FROM MYTABLE; $result = mysql_query($query); $nrows = mysql_num_rows($result); for ($i=0; $i$nrows; $i++) { $arr = mysql_fetch_array($result); $array[$i]['id'] = $arr['id']; $array[$i]['name'] = $arr['name']; } I know I can have a similar 'for' loop to step through the array, from 0 to count($array) But I would like to do something more elegant, with a foreach loop. But I have been unsuccessful as a foreach seems appropriate only with a single dimensional array. Can someone suggest a more elegant solution? Thanks, Rich P.S. yes, I am fairly new to PHP, less than 6 months. Probably my array loading method could be improved also! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] multidimensional arrays
Plutarck wrote: PHP can be a tad screwy with how it handles multi-dimensional arrays, but yes PHP handles them. No real speed problems with them either. But you may just want to use an associative array like: $loc = array("y" = $y, "x" = $x); Then just use $loc["y"] and $loc["x"]. Just another option, but feel free to use multi-dimensional arrays. Just be aware that PHP supports only two dimensions (so $array[][][] will not work), and if you try and get fancy with sort() and count() you are going to give yourself a migraine. $a[1][2][3][4][5] = "6"; echo $a[1][2][3][4][5]; $b[][][][]= "7"; echo $b[0][0][0][0]; I get '6' and '7'. Am I doing something wrong? This seems like PHP supports arrays with dimensions greater than 2. -- 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] multidimensional arrays
$a[1][2][3][4][5] = "6"; echo $a[1][2][3][4][5]; $b[][][][]= "7"; echo $b[0][0][0][0]; I get '6' and '7'. Am I doing something wrong? This seems like PHP supports arrays with dimensions greater than 2. You just need to look at PHP's structure. It makes total sense when you think about it. You can through arrays into cells in an array without any troubles. I've had arrays go 4 and 5 deep before just because there was an array I needed to put in a cell that related to that set of data. It's not wrong and I regard it as a feature. --Joe /* Joe Stump * Sr. PHP Developer * http://www.Care2.com http://www.joestump.net http://gtk.php-coder.net */ -- 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]