Simple problems with PHP arrays

3

to the point, I declared an empty array, in which I intend to save random integer values using a function and this is what I did, but when doing the count ($ array), I get 0 elements.

Example:

$numerosJ1 = array();
numerosJugadores($numerosJ1);

function numerosJugadores($arreglo) {
    for ($i = 0; $i <= 5; $i++) {
        $numero = rand(1, 45);
        $arreglo[$i] = $numero;
        echo $numero." ";
    }
}

echo count($numerosJ1);
    
asked by idionisio 23.09.2018 в 09:03
source

3 answers

3

Very simple: you are passing $numerosJ1 per copy ; that is, inside your function numerosJugadores( ) a copy of your array is arriving, no the original array; within that function you add things ... but to the copy , not the original.

The consequence of this: your original array $numerosJ1 does not change , since you are not really using it.

To solve it, simply pass it by reference (indicating it when declaring the function):

$numerosJ1 = array( );
numerosJugadores( $numerosJ1 );

function numerosJugadores( &$arreglo ) {
  for( $i = 0; $i <= 5; $i++ ) {
    $numero = rand( 1, 45 );
    $arreglo[$i] = $numero;
    echo $numero." ";
  }
}

echo count( $numerosJ1 );

As you see, the only change has been that, indicate to the PHP interpreter that the function requires passing the argument as a reference:

function numerosJugadores( &$arreglo ) {

By doing so, a copy is not created, and your function works with the original array .

    
answered by 23.09.2018 / 09:19
source
3

I think it's simpler to do this. At least with the naked eye there is less transfer of variables and you may gain in performance:

$numerosJ1 = numerosJugadores();

function numerosJugadores() {
    $arreglo=array();
    for ($i = 0; $i <= 5; $i++) {
        $numero = rand(1, 45);
        $arreglo[$i] = $numero;
        echo $numero." ";
    }
return $arreglo;
}

echo count($numerosJ1);

At least it's how I usually solve this type of problem.

Regarding the solution that @Trauma proposes, which by the way should be written indicating in the function, not in the call to it , that the value we pass is by reference .. .

$numerosJ1 = array();
numerosJugadores( $numerosJ1 );

function numerosJugadores( &$arreglo ) {
  for( $i = 0; $i <= 5; $i++ ) {
    $numero = rand( 1, 45 );
    $arreglo[$i] = $numero;
    echo $numero." ";
  }
}

echo count( $numerosJ1 );

... I am assailed by the doubt about what form would be most suitable to do what you want. It would perhaps be a reason for an interesting question.

    
answered by 23.09.2018 в 11:45
3

You could simplify the program by generating only what you need within the function in a single block of code. Passing an array by reference in this case is not necessary. It is better to verify if the function needs to be parameterized and in what way it affects general readability.

function numerosJugadores() { 
    // no se declaran variables internamente
    return array_map(function() {  return rand(1,45); }, range(0,6));
}

$jugadores = numerosJugadores(); // una sola llamada
    
answered by 23.09.2018 в 12:32