pocobueno1388 Posted January 16, 2007 Share Posted January 16, 2007 I have entries in the database that look like this:[code]200 Nails<br>710 Brick<br>340 Wood<br>200 Insulation<br>20 Carpet<br>5 Fibre Glass[/code]That is exactly what one column in the database holds. What I want to do is separate all these out into separate items and numbers. I want an array to hold each number and item separated but I want it to know which number goes with which word.So what I first did was separated them out into separate items and numbers. EX. 200 Nails.[code]$name=explode("<br>", $row['resourcesneeded']); //The $row['resourcesneeded'] holds what I posted above...the entire thing put together.[/code]So that gave me a list like such:[tt]200 Nails710 Brick340 WoodEtc.[/tt]Now I would like the number separated from the word. Like 200 and nails would be two separate strings, it would just know that those numbers went together. Here is the code that I have so far that just separates them into "200 Nails", "710 Brick":[code]$name=explode("<br>", $row['resourcesneeded']);$amountIn = count($name);for($x = 0; $x <= $amountIn; $x++){ print "$name[$x]<p>";}[/code]Hopefully I made sense...it is kinda complicated. Thanks in advanced to everyone who helps, it is much appreciated xP Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/ Share on other sites More sharing options...
dgiberson Posted January 16, 2007 Share Posted January 16, 2007 Im not sure why you have it setup this way but anyways, this should do the trick for ya....$name=explode("<br>", $row['resourcesneeded']);$amountIn = count($name);for($x = 0; $x <= $amountIn; $x++){ $line = explode(" ", $name[$x]); echo $line[0]; // prints 200 echo " ".$line[1]; // prints <space>nails} Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162327 Share on other sites More sharing options...
pocobueno1388 Posted January 16, 2007 Author Share Posted January 16, 2007 Thank you very much ^^ Worked beautifully.It wasn't me that set it up that way, it was my partner...I would have definitely done things differently. Thats okay though, this solves the problem.Thanks. Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162329 Share on other sites More sharing options...
sasa Posted January 16, 2007 Share Posted January 16, 2007 try[code]$name=explode("<br>", $row['resourcesneeded']);$amountIn = count($name);for($x = 0; $x < $amountIn; $x++){ print "$name[$x]<p>"; $out = explode(' ',$name[$x],2); print 'number is ',$out[0],' and word is ',$out[1];}[/code]for 5 Fibre Glass it output 5 FibreAdd 2 in explode function Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162334 Share on other sites More sharing options...
pocobueno1388 Posted January 16, 2007 Author Share Posted January 16, 2007 Ack...I have one more problem.Is there anyway I can access each variable separately? Right now the only way to print them out is print the entire list at once...I need to be able to call each indivdual string separately. Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162338 Share on other sites More sharing options...
GingerRobot Posted January 16, 2007 Share Posted January 16, 2007 My version. I think the others are going to run into troubles with the fibre glass because it has a space in the nameThis code:[code]<?php$row['resourcesneeded'] = "200 Nails<br>710 Brick<br>340 Wood<br>200 Insulation<br>20 Carpet<br>5 Fibre Glass";$name=explode("<br>", $row['resourcesneeded']);$amountIn = count($name);$numbers = array();$items = array();for($x = 0; $x < $amountIn; $x++){ $strpos = strpos($name[$x]," "); $number = substr($name[$x],'0',$strpos); $item = substr($name[$x],$strpos); $numbers[] = $number; $items[] = $item;}print_r($numbers);echo '<br />';print_r($items);?>[/code]Produces:Array ( [0] => 200 [1] => 710 [2] => 340 [3] => 200 [4] => 20 [5] => 5 ) Array ( [0] => Nails [1] => Brick [2] => Wood [3] => Insulation [4] => Carpet [5] => Fibre Glass ) Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162339 Share on other sites More sharing options...
dgiberson Posted January 16, 2007 Share Posted January 16, 2007 ^^ that's the way to go Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162343 Share on other sites More sharing options...
GingerRobot Posted January 16, 2007 Share Posted January 16, 2007 Also, you can access each one invididually using my version. There is an array containing each of the information. For instance, $items[0] contains Nails, $numbers[0] contains 200 etc Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162344 Share on other sites More sharing options...
pocobueno1388 Posted January 16, 2007 Author Share Posted January 16, 2007 GingerRobot - So so close. The output of your script gave me:[code]array()array()[/code]I tried this and it dispays nothing:[code]$numbers = array();$items = array();for($x = 0; $x < $amountIn; $x++){ $strpos = strpos($name[$x]," "); $number = substr($name[$x],'0',$strpos); $item = substr($name[$x],$strpos); $numbers[] = $number; $items[] = $item;}echo $numbers[0];[/code]Am I going about this the wrong way trying to access them? Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162353 Share on other sites More sharing options...
GingerRobot Posted January 16, 2007 Share Posted January 16, 2007 Did you forget these two lines:[code]$name=explode("<br>", $row['resourcesneeded']);$amountIn = count($name);[/code]If you dont include those, you'll get the output of two empty arrays. Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162360 Share on other sites More sharing options...
pocobueno1388 Posted January 16, 2007 Author Share Posted January 16, 2007 Yes, I included those. I just posted the part of the script not working, sorry. But yes, that part is still there. Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162364 Share on other sites More sharing options...
pocobueno1388 Posted January 16, 2007 Author Share Posted January 16, 2007 Your right! I did leave that out, sorry about that. It works now.Thank you very very much. Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162369 Share on other sites More sharing options...
GingerRobot Posted January 16, 2007 Share Posted January 16, 2007 No problem. Glad to help Link to comment https://forums.phpfreaks.com/topic/34466-need-help-with-separating-an-array/#findComment-162371 Share on other sites More sharing options...
Recommended Posts