Concatenate parts of variable names

Asked

Viewed 209 times

1

I have a cycle for, and I want to create several arrays with the name different from the increment of the for. I try that, but it’s not possible.

for($y=0; $y<=1; $y++){
    $newarray_date.$y = array();
    $newarray_time.$y = array();
    $newarray_valeur.$y = array();
}

In this case the name of the array would be $newarray_date0 and $newarray_date1.

  • 3

    I still don’t understand what you want old.... tries to give some examples to see if the question gets more entendivel

  • I don’t think you can do what you want. What akm wants is to create variables (arrays) with different names in a cycle. For example, there when y==0 creates the array with the name: $newarray_date0 and when y==1 creates the array with the name $newarray_date1

  • So@akm, try to give an example of what you want to do, maybe there’s another plausible solution

  • What you want is to generate arrays dynamically?

  • Guy I think enters another array there in history array("date0"=>array()...)

1 answer

10


You can’t do that, at least not directly. And even with the gambit you can do, it will produce one of the most bizarre codes I’ve ever seen.

If you call all variables of the same name you should use a array bidimmensional.

$newarray_date = array();
$newarray_time = array();
$newarray_valeur = array();
for($y = 0; $y <= 1; $y++){
    $newarray_date[$y] = array();
    $newarray_time[$y] = array();
    $newarray_valeur[$y] = array();
}

What you’re doing there is putting arrays inside in one another array, Porting it simulates having two dimensions (PHP does not have the actual concept of array multidimensional).

Whenever you want a variable name to vary the solution is to create an index for this name and the index is possible through arrays.

Ideas are coming up that use skulls. I thought of the two I’ve read here. Please do not use a complicated solution that can generate undesirable effects.

Variable variables are not true variables, complete, they cannot be used in any situation. They subvert what is expected of normal code, they hinder maintenance.

Arrays associative are at least makes explicit that you are not using variables but rather indices to simulate the variable. But it gets a weird code.

I would have no problem using any of these solutions if the problem really required one of them. But there is a more simple, easy, correct and elegant solution. So there is no reason not to use it.

You can still improve even more and make all this one variable. There are three possible solutions:

  1. Create another dimension to bring date, time and valeur as numerical indexes. It’s an attempt at optimization but it doesn’t help much in PHP. Not helping to identify each element well, the code would be using magic numbers.

  2. Create a class with date, time and valeur as members of it and use instances of this class as an element. This somewhat reverses the logic used but it seems to me that it makes more sense since I understand that these three values are inseparable. Data structures should be preferred in these cases to give more semantics to the code.

  3. Creates a new dimension like array associative. Probably preferred because it is less ceremonious than creating a class just for this while maintaining a good semantics. It would look something like this:

    $newarray = array();
    for($y = 0; $y <= 1; $y++){
        $newarray[$y]['date'] = array();
        $newarray[$y]['time'] = array();
        $newarray[$y]['valeur'] = array();
    }
    

I put in the Github for future reference.

  • 4

    Who the gambit get registered in fine print then :)

  • Good idea! : ) The question is so strange that I didn’t even think of such a simple answer. But I think the idea of AP is really to create variables in a cycle. Anyway it’s a great idea. Again the answer saves the question ;)

  • 2

    @Jorgeb. I’m so used to seeing crazy codes that I get these intentions soon.

  • 1

    @bfavaretto hope that the ups in your comment are for only the part of the fine print :)

  • @bigown there goes that boring question that every programmer asks himself one day.... should we go the easy way, or the right way? Because the easiest sometimes also seems to be the most correct in most cases

  • I’ve seen this gambit used instead of extract. Kind of foreach($array as $key => $value){ $$key = $value; }

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.