NoobNewbie Posted July 9, 2007 Share Posted July 9, 2007 Hello! I'm really new to php and I was wondering if you could help in the following situation. If I have an array called my_array1, how do I call it within a for cycle? Something like this: for($a=1;$a<=5;++$a){ $my_array = 'my_array'.$a; //prints $my_array } I know that doesnt work, so, I tried something like the following: for($a=1;$a<=5;++$a){ $my_array = 'my_array'.$a; //prints $$my_array } That does work with variables, but doesnt work with arrays. I know that in Java and ActionScript what I'm trying to acomplish is pretty simple, but I cant seem to find the correct syntax for php. Help? Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 First, surround code with [code] and [/code] tags to make it easier to read. What exactly are you wanting to do with your array? You can cycle through it with a foreach() loop like this: <?php foreach ($array as $myvar) { echo 'Array contents is '.$myvar.'<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
NoobNewbie Posted July 9, 2007 Author Share Posted July 9, 2007 Hi Yesideez. Thanks for answering. What I want is to be able to call arrays from within a for cycle. For example: my_array1 = array("something","something"); my_array2 = array("something","something"); my_array3 = array("something","something"); for($a=1;$a<=3;++$a){ //does something with the array that corresponds to $a } Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 If you want to specifically use a for() loop this would do it: <?php $my_array=array('something','something'); for ($a=0;$a<count($my_array);$a++) { echo $my_array[$a].'<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
NoobNewbie Posted July 9, 2007 Author Share Posted July 9, 2007 But that will just write the array, right? But what I wanted was to be able to call a specific array and do whatever I want with him. In other languages I do something like this: my_array1 = array("something","something"); my_array2 = array("something","something"); for ($a=0;$a<2;$a++) { print eval('my_array' + $a)[0]; } ..and it will print the first element of my_array1 and the first element of my_array2. I'm not really seing how to do that in php. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 To be honest I'm not entirely sure what it is you want to do with your array - are yuo storing code inside it? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 9, 2007 Share Posted July 9, 2007 Well, you can use variable variables: <?php $var1 = 'foo'; $var2= 'bar'; for($x=1;$x<=2;$x++){ $var_name = 'var'.$x; echo $$var_name.'<br />'; } ?> However, you are probably better off using multidimensional arrays. For example, this: <?php $arrays = array(array('something','somethingelse'),array('anothersomething','somethingmore')); echo '<pre>'; print_r($arrays); echo '</pre>'; ?> Would produce: Array ( [0] => Array ( [0] => something [1] => somethingelse ) [1] => Array ( [0] => anothersomething [1] => somethingmore ) ) As you can hopefully see, you could cycle through the various "dimensions" of the array. Quote Link to comment Share on other sites More sharing options...
NoobNewbie Posted July 9, 2007 Author Share Posted July 9, 2007 Well that may be a good option, GingerRobot! And, then, to adress a specific element from within the multidimensional array I call it like a matrix, something like this... $arrays = array(array('something','somethingelse'),array('anothersomething','somethingmore')); echo $arrays[0][1]; ..and it prints 'somethingelse'. Thanks! It's a great way! 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.