fife Posted February 6, 2012 Share Posted February 6, 2012 I have a variable on my page called, $recipe['ingredients']; inside the var you have for example.... 100ml milk, 350ml double cream, 150ml water and so on. Now Im trying to split it up so it looks as follows <ul> <li>100ml milk</li> <li>350ml double cream</li> <li>150ml water</li> </ul> So far I have the following code..... $ingredientsParts = explode(',', $row_rs_recipes['ingredients']); $ingredients = array($ingredientsParts); while (! $ingredients) { echo" <li>$ingredients</li>";} But for some reason it doesnt work and I do not have the exp with explode to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/256534-split-an-array-into-chunks/ Share on other sites More sharing options...
Psycho Posted February 6, 2012 Share Posted February 6, 2012 1. When you explode() a string it is automatically converted into an array. You do not need to convert it to an array type as you did on the second line. 2. You want to use a foreach() loop to iterate through an array, not a while loop. $ingredientsAry = explode(',', $row_rs_recipes['ingredients']); foreach($ingredientsAry as $ingredient) { echo "<li>$ingredient</li>"; } In fact you can just do a foreach() loop on the explode() value foreach(explode(',', $row_rs_recipes['ingredients']) as $ingredient) { echo "<li>$ingredient</li>"; } Quote Link to comment https://forums.phpfreaks.com/topic/256534-split-an-array-into-chunks/#findComment-1315115 Share on other sites More sharing options...
fife Posted February 6, 2012 Author Share Posted February 6, 2012 thankyou works great!! Quote Link to comment https://forums.phpfreaks.com/topic/256534-split-an-array-into-chunks/#findComment-1315123 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.