Variable array assignment

Asked

Viewed 956 times

2

There is a correct way to do this highlighted code snippet?

$numeros = range(0, 9);
shuffle($numeros);
$id = array_slice($numeros, 1, 9);

$mult = $id[0].$id[1].$id[2].$id[3].$id[4].$id[5].$id[6].$id[7].$id[8];

echo $mult;

that is, my intention is to transform a array in a simple variable for later use, in this case

$mult = $id[0].$id[1].$id[2].$id[3].$id[4].$id[5].$id[6].$id[7].$id[8];

could be done in a more professional way?

  • What do you call the most professional way? You want to do without repeating the variable 9 times?

  • exactly @bigown , I was wondering if there’s a way to just not repeat 9 times the same thing.

3 answers

3

Another way to convert an array for a string to use implode, the first argument is the array to be converted, the second would be delimiter that would be between the array items, in which case you can use nothing('') so the generated string will be a number of 10 digits.

<?php
$numeros = range(0, 9);
$str_numero = implode($numeros, '');
echo $str_numero;

phpfiddle - example

  • really worked very well, implementing the shuffle it became complete $rand_rules = range(0, 9);&#xA; shuffle($rand_rules);&#xA; $rand_rules = array_slice($rand_rules, 1, 9);&#xA; $str_numero = implode($rand_rules, "");&#xA; echo $str_numero."\n";

2


I think this is what you want:

$numeros = range(0, 9);
shuffle($numeros);
$id = array_slice($numeros, 1, 9);
$mult = "";
for ($i = 0; $i < 9; $i++) {
    $mult .= $id[$i];
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

This way you are making the variable repeat nine times automatically. You are creating a repeat loop. So there in the foryou are counting from 0 to 9, incrementing one by one, so every step through the loop it picks a different index from the array as the count variable of the loop. Each pass it concatenates the new element of the array to the variable that will receive the final result.

The result is exactly the same as what you did and this way is called Fisher-Yates.

Documentation of for and of array for more references and study.

  • exactly that, thank you very much, in case the work is being done on $mult .= ?

  • Actually in the last 4 lines. It’s a whole, you can’t analyze just one line.

  • o .= would be concatenating the current value with the new value of the next, exact? loop without the . it would delete the current one and switch to the new one?

  • It does not erase, it concatenates, and concatenate and add one text to another already exists. It is not replacing, it is adding. Then it accumulates one after the other. If you change the expression $i < 9 for a smaller number, you can start at 1 and see what happens. Then you go up until you reach the 9. To better visualize you can put a line with echo $mult. "\n"; inside the loop to see how it evolves in every step.

  • Yes, I tried but told me to wait about 5 minutes, but then, that’s what I mentioned, the .= stitch does the work of concatenating, the rest I already knew, I just didn’t realize I could by one . stitch before = equal to work

  • This is called composite allocation operator. It’s the same as doing $mult = $mult . $id[$i];. Rray gave another way to do it. I thought about it but I avoided it because I realized that you are learning, I thought it best to teach you the basics before you get all chewed up. I think you should understand what you’re doing, how to get the result. The internally implode function works very similar to what I posted to you.

Show 1 more comment

-1

  • -1? Care to explain why?

Browser other questions tagged

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