[SOLVED] split() in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

$tst="3315|Pencils|2|10.24|ON|3320|Textas|2|15.32|ON||||||||";
print_r(getProductArray($tst));
function getProductArray($string){
    $res=array();
    $str=false;
    do{
        $res[]=explode("|",$string,6);
        $str=end($res);
        $string=strlen($str[5])?$str[5]."|":"";
    }while($string);
    foreach($res as $id=>$row) unset($res[$id][5]);
    return $res;
}
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

awesome... It works...

But how do I pull individual result out of that array?
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

Nevermind, I figured it out... Thanks!
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

I'm having trouble figuring out how to get each line out in a variable from that array?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

foreach($result_array as $line){
    //.... do something with line
}
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

Continuing on from the example you gave above, this is what I have:


foreach($res as $line)
{

$articleNum = $line[1];
$productPackage = $line[2];
$quantity = $line[3];
$price = $line[4];
$gst = $line[5];

I think I may have thrown myself in too deep... lol. Thanks for your help.
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

This is the only thing I'm still stuck on now
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

heh, Uni, I don't understand your requirements. English isn't my native tongue :D so could you explain in details what do you need to extract from that array?
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

Code: Select all

<?php
while($i != 9) 
{ 

$articleNum = $results[$i][1]; 
$productPackage = $results[$i][2]; 
$quantity = $results[$i][3]; 
$price = $results[$i][4]; 
$gst = $results[$i][5];

}
?>
That is continued from your example earlier in this thread... In the code above, I'm trying to extract each of the 5 pieces of information into a variable from the 10 lines that were put into the array.

Thanks for your help once again :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

UnimatrixZer0 wrote:

Code: Select all

<?php
while($i != 9) 
{ 

$articleNum = $results[$i][1]; 
$productPackage = $results[$i][2]; 
$quantity = $results[$i][3]; 
$price = $results[$i][4]; 
$gst = $results[$i][5];

}
?>
That is continued from your example earlier in this thread... In the code above, I'm trying to extract each of the 5 pieces of information into a variable from the 10 lines that were put into the array.
You're not trying, you're doing it ;) So what's wrong with the above code? (don't forget to increment $i variable in loop unless you really want infinite loop):

Code: Select all

while($i != 9) 
{ 
  $articleNum = $results[$i][1]; 
  $productPackage = $results[$i][2]; 
  $quantity = $results[$i][3]; 
  $price = $results[$i][4]; 
  $gst = $results[$i][5];
  
  //do something
  $i++; //increment the $i
}
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

The problem is that it returns nothing from the results array.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try this just before the while loop then post the results here so we can see what, if anything the results array contains

Code: Select all

echo "<pre>";
print_r($results);
echo "</pre>";
Mark
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

Nevermind... Figured out that the $results array was inside a function, therefore not global... And when called on outside that function contained nothing...

Silly me. lol
UnimatrixZer0
Forum Newbie
Posts: 17
Joined: Wed Dec 03, 2003 5:06 am

Post by UnimatrixZer0 »

It works! Thanks to all who helped with this question! :D
Post Reply