Jump to content

[SOLVED] Changing an array name.


NoobNewbie

Recommended Posts

Hello! ;D

 

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?

 

 

Link to comment
Share on other sites

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 />';
  }
?>

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.