mongoose00318 Posted July 28, 2010 Share Posted July 28, 2010 Hi all! Ok I am trying to put a delimited list like so , EX. item qty, item name, item price | item qty, item name, item price | item qty, item name, item price | etc. into an array so I can access it like this - $product[0] = qty, $product[1] = name, etc. My code just isnt working. This is what I have so far. $prod = array(); //breaking products text down for display $products1 = explode("|", $products); $num_prod1 = count($products1); $count = 0; foreach($products1 as $p) { $prod[] = $p; $products2 = explode(",", $p); foreach($products2 as $p2) { $prod[$count] = $prod[$count][$p2]; } $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/209165-taking-a-delimited-list-and-putting-it-into-an-array/ Share on other sites More sharing options...
simshaun Posted July 28, 2010 Share Posted July 28, 2010 <?php $string = '1,Foo,$1.00|1,Bar,$3.00'; $products = array(); $_products = explode('|', $string); foreach ($_products AS $product) $products[] = explode(',', $product); echo "<pre>"; print_r($products); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/209165-taking-a-delimited-list-and-putting-it-into-an-array/#findComment-1092377 Share on other sites More sharing options...
mongoose00318 Posted July 28, 2010 Author Share Posted July 28, 2010 foreach($products1 as $p) { $products2 = explode(",", $p); $count = 1; foreach($products2 as $p2) { if($count <= 2) { echo $p2." | "; $count++; } else { echo $p2."<br>"; $count = 1; } } } That code above successfully processes the delimited list I have, is there a way I can modify that to make the array I need? Any advice would be helpful! Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/209165-taking-a-delimited-list-and-putting-it-into-an-array/#findComment-1092379 Share on other sites More sharing options...
mongoose00318 Posted July 28, 2010 Author Share Posted July 28, 2010 Your a miracle worker simshaun! Thanks so much! I wish I could have figured that out... Quote Link to comment https://forums.phpfreaks.com/topic/209165-taking-a-delimited-list-and-putting-it-into-an-array/#findComment-1092381 Share on other sites More sharing options...
mongoose00318 Posted July 28, 2010 Author Share Posted July 28, 2010 Mark as solved! Quote Link to comment https://forums.phpfreaks.com/topic/209165-taking-a-delimited-list-and-putting-it-into-an-array/#findComment-1092382 Share on other sites More sharing options...
Alex Posted July 28, 2010 Share Posted July 28, 2010 You can do that yourself. There is a button at the bottom left to do so. Quote Link to comment https://forums.phpfreaks.com/topic/209165-taking-a-delimited-list-and-putting-it-into-an-array/#findComment-1092384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.