Re: Re: [PHP-DB] numeric string to single digit array

From: Date: Wed, 26 Mar 2008 19:46:40 +0000
Subject: Re: Re: [PHP-DB] numeric string to single digit array
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-44817@lists.php.net to get a copy of this message
Not sure if this is relevant anymore, but... > i.e. "1223123" into > ['1','2,','2','3','1,'2','3'] ? $num = "1223123"; $nums = array_filter(preg_split('//', $nums)); Or you can use this function. It's probably better since the array_filter will probably get rid of any 0's in the string. function string_to_array($str) { $arr = preg_split('//', $str); array_shift($arr); // removing leading null from split array_pop($arr); // remove training null from split return $arr; } Or, if you just want the numbers from your student id (and you wanted only numbers): $num = "A123456789"; $nums = string_to_array(preg_replace("/[^0-9]+/", "", $nums)); - Jon L. On Wed, Mar 26, 2008 at 1:35 PM, Richard Dunne <richarddunne1971@o2.ie> wrote: > Using this extract from > http://ie.php.net/manual/en/control-structures.foreach.php > Amaroq > 09-Mar-2008 06:40 > Even if an array has only one value, it is still an array and foreach will > run it. > > <?php > $arr[] = "I'm an array."; > > if(is_array($arr)) > { > foreach($arr as $val) > { > echo $val; > } > } > ?> > > The above code outputs: > I'm an array. > ------------------------------------- > So if I use: > > $query = "Select answer from answers where studentID='A123456789'"; > $result = mysql_query($query,$connection) or die(mysql_error()); > while($row = mysql_fetch_assoc($result)) > { > foreach($row as $answer) > { > echo $answer . "\n"; > } > } > I thought I would be able to print out each array element, but I am not > getting any output. Has anyone got a better idea? > > > ---------- Forwarded message ---------- > From: Evert Lammerts <evert.lammerts@gmail.com> > To: Richard Dunne <richarddunne1971@o2.ie> > Date: Wed, 26 Mar 2008 18:22:53 +0100 > Subject: Re: [PHP-DB] numeric string to single digit array > > > Tried that as well and got the same result. > > I tried "Select count(answer) as total from answers where > studentID='A123456789'"; > > from the CLI and got total = 2 as a result. > > > > So, we got rid of the Invalid Resource error and we know that the > student id you use occurs in both rows in your table and that your query > works fine. > > Did you get rid of the semicolon @ line 15 "while($row = > mysql_fetch_assoc($result));", as Jason suggested? Also, an: > error_reporting(E_ALL); > at the top of your code might help in backtracing. > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.db (#44817) next »