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! Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/ 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! Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/#findComment-688931 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]); } ?> Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/#findComment-688932 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 Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/#findComment-688935 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. Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/#findComment-688938 Share on other sites More sharing options...
corbin Posted November 12, 2008 Share Posted November 12, 2008 Hrmmmm nvm. Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/#findComment-688942 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); } ?> Link to comment https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/#findComment-688952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.