Jump to content

Need help with separating an array.


pocobueno1388

Recommended Posts

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 Nails
710 Brick
340 Wood
Etc.
[/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
Share on other sites

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 "&nbsp".$line[1]; // prints <space>nails


}
Link to comment
Share on other sites

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 Fibre
Add 2 in explode function
Link to comment
Share on other sites

My version. I think the others are going to run into troubles with the fibre glass because it has a space in the name

This 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
Share on other sites

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
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.