Define an array with default values in PHP

0

Is there a simplified way to define an array and set it to default values? For now I have the following:

$arrData = [];
for($i=0; $i<20; ++$i) $arrData[] = 0;

Any ideas? I know that in Java and C ++ you can but in PHP I do not know.

    
asked by caicedo1089 06.06.2018 в 19:10
source

4 answers

2

What you need is the function array_fill() or you might prefer array_fill_keys() . Each of these functions returns an array, just like the function Array() , only that instead of, explicitly, providing a value to each element, you provide (in addition to the value you want to be repeated in all its elements) the amount of elements (and their initial index) for array_fill() or the collection of indexes for array_fill_keys() .

Your example would be translated as follows:

$arrData = array_fill(0, 20, 0);
// Digase: (Indice inicial, cantidad de elementos, valor de dichos elementos)

I leave the links so you can look at the documentation better, but I quote array_fill() :

  

Fill an array with $num entries of the value of the parameter $value , the keys start at the $start_index parameter.

Syntax:

  

array array_fill ( int $start_index , int $num , mixed $value )

Also, if you wanted to "impersonate" all the values of an existing array by a single value, the use of array_fill_keys() goes into action as follows:

$arrData = array_fill_keys(array_keys($arrData), 1);
// Digase: (arreglo de indices, valor para dichos indices)

In this case I used the function array_keys() to return an array of current indices from an existing array. In this case the existing array was $arrData , and therefore I returned an array with the same indexes but loaded with the value 1 in all of them, this array I keep in $arrData and therefore I say that "supplant" their existing values (although not strictly true).

    
answered by 06.06.2018 / 19:45
source
0

So you were half good

$arrData = array();
for( $i = 0; $i < 20; $i++ ){
 $arrData[] = $i;
 //Tambien puedes usar $arrData[$i] = $i;
}
var_dump( $arrData );

You just lacked to declare the variable as an arrangement.

    
answered by 06.06.2018 в 19:16
0

You can do it in the following way using array_push , this makes insert one or more element at the end of the array.

$arrData = array();
for($i=0; $i<20; $i++)
{
    array_push($arrData,$i);
}

print_r($arrData);
    
answered by 06.06.2018 в 19:19
0

There are several ways to do it, PHP is very flexible and does part of the work for us if we leave it.

To initialize an empty array:

$arr = [];
//O también puedes
$arr = array();

One way to fill it would be something like:

$arr[] = "perro";
$arr[] = "gato";
$arr[] = "pato";

Here PHP does the magic of assigning the value to the next box, that is, it would be the same thing to do:

$arr[0] = "perro";
$arr[1] = "gato";
$arr[2] = "pato";

For more complex objects you can use the following structure:

$arr = [
    "foo" => "pato",
    "bar" => "perro",
];

You can also initialize the fix simultaneously in this way:

$arr=array(10,20,30,40,50);

And from there you can do crazy things like JSON or complex structures:

$arr=array(6,"santosh","rahul",array("x","y","z"));

To keep in mind, the version of PHP you are using makes it possible to use one or other forms, especially Before and After 5.4. All the examples I gave you are for later versions of this.

Finally, a link to see the different functions you can apply to PHP fixes, always taking care of the version you are using.

    
answered by 06.06.2018 в 19:21