scuff Posted November 12, 2008 Share Posted November 12, 2008 Ok I have a code: <?php $array1 = array(); $array1[0] = "hey hey"; $array1[1] = "hey hey"; $counter = count($array1); for ($p = 0; $p < $counter; $p++) { $arraynew.$p = explode(" ", $array1[$p]); } ?> Basically what I want it to do is create a new array for each one of the array values. But the problem is that $arraynew.$p isn't working, it just sets the array to $p instead of what I want: arraynew0 then arraynew1 etc.. If you have any questions or anything just ask. thanks! Quote Link to comment Share on other sites More sharing options...
NikkiLoveGod Posted November 12, 2008 Share Posted November 12, 2008 you have to make a variable variable out of it, for example. Here's how. <?php $array1 = array(); $array1[0] = "hey hey"; $array1[1] = "hey hey"; $counter = count($array1); for ($p = 0; $p < $counter; $p++) { $varname = 'arraynew' . $p; $$varname = explode(" ", $array1[$p]); } ?> This way you'll have an varible of $arraynew0 that contains array with [0] = hey and [1] = hey. Hope this helps! Quote Link to comment Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 <?php $array1 = array(); $array1[0] = "hey hey"; $array1[1] = "hey hey"; $counter = count($array1); for ($p = 0; $p < $counter; $p++) { ${'arraynew'.$p} = explode(" ", $array1[$p]); } ?> Quote Link to comment Share on other sites More sharing options...
scuff Posted November 12, 2008 Author Share Posted November 12, 2008 Thanks both of you, but I think I will use thorpes since it's shorter Quote Link to comment Share on other sites More sharing options...
NikkiLoveGod Posted November 12, 2008 Share Posted November 12, 2008 wow, you can really use ${'something' . $var} and it works!? I have never known that! Looks like you always learn something new And yea, use thorpe's, it lookes a ton faster to parse aswell. Quote Link to comment Share on other sites More sharing options...
corbin Posted November 12, 2008 Share Posted November 12, 2008 Hrmmmm nvm. Quote Link to comment Share on other sites More sharing options...
trq Posted November 12, 2008 Share Posted November 12, 2008 And yea, use thorpe's, it lookes a ton faster to parse aswell. It would actually be quicker to simply create an array of arrays. <?php $array1 = array(); $array1[0] = "hey hey"; $array1[1] = "hey hey"; $counter = count($array1); $array2 = array(); foreach ($array1 as $value) { $array2[] = explode(' ', $value); } ?> Quote Link to comment 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.