array_fill


php128 apg

PRODUCES an ARRAY through the sequential REPEAT of the same ELEMENT.





If $start_index < 0 the first index of the returned array will be $start_index and the following indices will start from zero.

$start_index must be greater than or equal to zero.

This function issues an E_WARNING if $num < 0.



<?php

arr array_fill 
( int $start_index , int $num , mix $value )
 

where,

$start_index = The first index of the returned ARRAY

$num = The number of elements to insert

$value 
= The value to be used for filling

?>
 

$start_index


The first index of the returned ARRAY .



$num


The number of elements to insert .



$value


The value to be used for filling .



  1 EXERCISE   

<?php

$val01fl 
= [ 2, 4, 16 ];

$st01ndx = 0;

$nr01val = 3;

echo 
'The given $val01fl:<br><pre>';
print_r($val01fl);
echo 
'</pre>The start index: ' . $st01ndx . '<br><br>';

echo 
'Number of $val01fl to fill: ' . $nr01val;

echo 
'<br><br>After array_fill($st01ndx, $nr01val, $val01fl):<br><pre>';

$arr01fld = array_fill($st01ndx, $nr01val, $val01fl);
print_r($arr01fld);
echo 
'</pre>';

?> 

  2 EXERCISE   

<?php

$POR_locale 
= ['pt-pt', "pt_PT", "ptg", "portuguese"]; 

setlocale (LC_ALL, $POR_locale); 

// Compatibility with LOCALE, 
// only takes effect on PHP versions below 8.
// Test, therefore, to verify this.

$val02fl = 88.32;

$st02ndx = 2;

$nr02val = 3;

echo 
'The given $val02fl:<br>';
var_dump($val02fl);

echo 
'<br><br>The start index: ' . $st02ndx;

echo 
'<br><br>Number of $val02fl to fill: ' . $nr02val;

echo 
'<br><br>After 
       array_fill(array_fill($st02ndx, $nr02val, $val02fl):<br>'
;
$arr02fld = array_fill($st02ndx, $nr02val, $val02fl);
echo 
'<pre>';
var_dump($arr02fld);
echo 
'</pre>';

?> 

  3 EXERCISE   

<?php

$val03fl 
= 'ECHO';

$st03ndx = mt_rand(0, 5);

$nr03val = mt_rand(0, 7);

echo 
'The given $val03fl:<br>';
var_dump($val03fl);

echo 
'<br><br>The start index, $st03ndx: ' . $st03ndx;

echo 
'<br><br>Number of $val03fl to fill: ' . $nr03val;

echo 
'<br><br>After 
       array_fill(array_fill($st03ndx, $nr03val, $val03fl):<br>'
;

$arr03fld = array_fill($st03ndx, $nr03val, $val03fl);
echo 
'<pre>';
print_r($arr03fld);
echo 
'</pre>';

?> 

  4 EXERCISE   

<?php

$val04fl 
= NULL;

$st04ndx = 100;

$nr04val = 5;

echo 
'The given $val04fl:<br>';
var_dump($val04fl);

echo 
'<br><br>The start index, $st04ndx: ' . $st04ndx;

echo 
'<br><br>Number of $val04fl to fill: ' . $nr04val;

echo 
'<br><br>After 
       array_fill(array_fill($st04ndx, $nr04val, $val04fl):<br>'
;

$arr04fld = array_fill($st04ndx, $nr04val, $val04fl);
echo 
'<pre>';
print_r($arr04fld);
echo 
'</pre>';

?> 

  5 EXERCISE   

<?php

$rd05 
= mt_rand(0, 1);

if(
$rd05 == 0)
{
$val05fl = false;
}
else
{
$val05fl = true;
}    

$st05ndx = 100;

$nr05val = 5;


echo 
'The given $val05fl:<br>';
var_dump($val05fl);

echo 
'<br><br>The start index, $st05ndx: ' . $st05ndx;

echo 
'<br><br>Number of $val05fl to fill: ' . $nr05val;

echo 
'<br><br>After 
       array_fill(array_fill($st05ndx, $nr05val, $val05fl):<br>'
;

$arr05fld = array_fill($st05ndx, $nr05val, $val05fl);
echo 
'<pre>';
var_dump($arr05fld);
echo 
'</pre>';

?> 

  7 EXERCISE   

<?php

$array1 
= [0.0, 1, 2.5];
$array2 = [TRUE, FALSE, NULL, "d", "e", "f"];

echo 
'The given $array1:<br>';
var_dump($array1);

echo 
'<br><br>The given $array2:<br>';
var_dump($array2);

foreach(
$array1 as $start)
{
foreach(
$array1 as $num)
{
foreach(
$array2 as $value)
{
echo 
'<br><br>';
echo 
'start: '.$start.' num: '.$num.' value: '.$value."<br>";
$output = array_fill($start, $num, $value);
var_dump($output);
}
}
}

?>

  8 EXERCISE   

<?php

echo "Testing array_fill() : basic functionality.<br>";

// calling the array_fill with all 
// possible valid values for 'val' argument
$start_key = 0 ;
$num = 2;
$heredoc = <<<HERE_DOC
Hello
HERE_DOC;

// array of possible valid values
// for 'val' argument
$values = array (

  
/* 1  */  NULL,
            
0,
            
1,
  
/* 4  */  1.5,
            
'hi',
            
"hi",
  
/* 7  */  $heredoc

);

$counter = 1;
for(
$i = 0; $i < count($values); $i ++)
{
  echo 
"<br><br>Iteration: $counter<br>";
  
$val = $values[$i];

  
var_dump( array_fill($start_key,$num,$val) );

  
$counter++;
}

?>

  9 EXERCISE   

<?php

/* 
* passing array_fill() as the 
* 'val' argument in array_fill() function 
*/

echo "Testing array_fill() : variation.<br>";

$start_key = 0;
$num = 2;
$heredoc = <<<HERE_DOC
Hello
HERE_DOC;

// array of possible valid values for 'val' argument
$values = array (

  
/* 1  */  NULL,
            
0,
            
1,
  
/* 4  */  1.0,
            
'hi',
            
"hi",
  
/* 7  */  $heredoc
);

echo 
"<br><br>Filling 2 dimensional array
                 with all basic valid values:"
;
$counter = 1;
for(
$i =0; $i < count($values); $i ++)
{
  echo 
"<br><br>Iteration: $counter<br>";
  
$val = $values[$i];

  
var_dump( array_fill($start_key,$num,
                   
array_fill($start_key,$num,$val)) );

  
$counter++;
}

?>

  10 EXERCISE   

<?php

/*
 * Testing array_fill() by passing 
 * different types of array  values
 * for 'val' argument
 */

echo "Testing array_fill() : usage variations.<br><br>";

// Initialise function arguments 
// not being substituted
$start_key = 0;
$num = 2;


// array of different types of 
// array values for 'val' argument
$values = array(

  
/* 1  */  array(),
            array(
1 , 2 , 3 , 4),
            array(
1 => "Hi" , 2 => "Hello"),
            array(
"Saffron" , "White" , "Green"),
  
/* 5  */  array('color' => 'red' , 'item' => 'pen'),
            array( 
'color' => 'red' , 2 => 'green ' ),
            array(
"colour" => "red" , "item" => "pen"),
            array( 
TRUE => "red" , FALSE => "green" ),
            array( 
true => "red" , FALSE => "green" ),
  
/* 10 */  array( 1 => "Hi" , "color" => "red" , 'item' => 'pen'),
            array( 
NULL => "Hi", '1' => "Hello" , "1" => "Green"),
            array( 
""=>1, "color" => "green"),
  
/* 13 */  array('Saffron' , 'White' , 'Green')
);

// loop through each element of the 
// values array for 'val' argument
// check the working of array_fill()
echo "With different types of array values for 'val' argument:";
$counter = 1;
for(
$i = 0; $i < count($values); $i++)
{
  echo 
"<br><br>Iteration: $counter<br>";
  
$val = $values[$i];

  
var_dump( array_fill($start_key , $num , $val) );

  
$counter++;
}

?>