array_fill_keys


php128 apg

PRODUCES an ARRAY through the repetition of the same ELEMENT, with INDEXATION specified by means of the ELEMENTS of another ARRAY.





This function fills an ARRAY with the value of the $value parameter, using the values of the $keys ARRAY as keys.

Ilegal values for $keys will be converted to STRING.



<?php

arr array_fill_keys 
( arr $keys , mix $value )
 

where,

$keys = The ARRAY of values to be used as keys

$value 
= The value to be used for filling

?>
 

$keys


The ARRAY of values to be used as keys.



$value


The value to be used for filling.



  1 EXERCISE   

<?php

$keys01 
= [ "ELEVEN", "12", 13 ];

$vals01 = [ 11, 12, 13 ];

echo 
'The given $keys01:<br>';
echo 
'<pre>';
var_dump($keys01);
echo 
'</pre><br>';

echo 
'The given $vals01:<br>';
echo 
'<pre>';
var_dump($vals01);
echo 
'</pre><br>';

echo 
'After array_fill_keys ($keys01, $vals01):<br>';
$arrkfld01 = array_fill_keys ($keys01, $vals01);
echo 
'<pre>';
var_dump($arrkfld01);
echo 
'</pre>';

?> 

  2 EXERCISE   

<?php

$keys02 
= [ "ELEVEN", "TWELVE"];

$vals02 = [ 11, 'twelve', 13, 'fourteen' ];

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

echo 
'<br></pre>The given $vals02:<br><pre>';
var_dump($vals02);

echo 
'</pre><br>After array_fill_keys ($keys02, $vals02):<br><pre>';
$arrkfld02 = array_fill_keys ($keys02, $vals02);
var_dump($arrkfld02);
echo 
'</pre>';

?> 

  3 EXERCISE   

<?php

$keys03 
= [ "ELEVEN", "TWELVE", "THIRTEEN"];

$vals03 = [ 11, 12 ];

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

echo 
'<br></pre>The given $vals03:<br><pre>';
var_dump($vals03);

echo 
'</pre><br>After array_fill_keys ($keys03, $vals03):<br><pre>';
$arrkfld03 = array_fill_keys ($keys03, $vals03);
var_dump($arrkfld03);
echo 
'</pre>';

?> 

  4 EXERCISE   

<?php

$keys04 
= [ "ELEVEN", "TWELVE", "THIRTEEN"];

$vals04 = 'THIRTY-TWO';

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

echo 
'<br></pre>The given $vals04:<br><pre>';
var_dump($vals04);

echo 
'</pre><br>After array_fill_keys ($keys04, $vals04):<br><pre>';
$arrkfld04 = array_fill_keys ($keys04, $vals04);
var_dump($arrkfld04);
echo 
'</pre>';

?>

  5 EXERCISE   

<?php

$keys05 
= [ "ELEVEN", "TWELVE", "THIRTEEN"];

$vals05 = NULL;

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

echo 
'<br></pre>The given $vals05:<br><pre>';
var_dump($vals05);

echo 
'</pre><br>After array_fill_keys ($keys05, $vals05):<br><pre>';
$arrkfld05 = array_fill_keys ($keys05, $vals05);
var_dump($arrkfld05);
echo 
'</pre>';

?>

  6 EXERCISE   

<?php

$keys06 
= [ "ELEVEN", "TWELVE", "THIRTEEN"];

$rnd06 = mt_rand(0, 6);

if(
$rnd06 == 0)
{
$vals06 = false;
}
elseif(
$rnd06 == 1)
{
$vals06 = true;
}    
elseif(
$rnd06 == 2)
{
$vals06 = ''; // EMPTY
}
elseif(
$rnd06 == 3)
{
$vals06 = ' '; // SPACE
}
elseif(
$rnd06 == 4)
{
$vals06 = ""; // EMPTY
}
elseif(
$rnd06 == 5)
{
$vals06 = " "; // SPACE
}

else
{
$vals06 = 4;    
}


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

echo 
'<br></pre>The given $vals06:<br><pre>';
var_dump($vals06);
echo 
'</pre>';

echo 
'Random Number: ' . $rnd06 . '<br><br>';

echo 
'<br>After array_fill_keys ($keys06, $vals06):<br><pre>';
$arrkfld06 = array_fill_keys ($keys06, $vals06);
var_dump($arrkfld06);
echo 
'</pre>';

?> 

  7 EXERCISE   

<?php

$keys07 
= [];

$vals07 = [ 105, 106, '107'];    

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

echo 
'<br></pre>The given $vals07:<br><pre>';
var_dump($vals07);

echo 
'</pre><br>After array_fill_keys ($keys07, $vals07):<br><pre>';
$arrkfld07 = array_fill_keys ($keys07, $vals07);
var_dump($arrkfld07);
echo 
'</pre>';

?> 

  8 EXERCISE   

<?php

$keys08 
= [];

$vals08 = [];    

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

echo 
'<br></pre>The given $vals08:<br><pre>';
var_dump($vals08);

echo 
'</pre><br>After array_fill_keys ($keys08, $vals08):<br><pre>';
$arrkfld08 = array_fill_keys ($keys08, $vals08);
var_dump($arrkfld08);
echo 
'</pre>';

?> 

  9 EXERCISE   

<?php

var_dump
(array_fill_keys(array(), 1));
echo 
'<br><br>';
var_dump(array_fill_keys(array('foo', 'bar'), NULL));
echo 
'<br><br>';
var_dump(array_fill_keys(array('5', 'foo', 10, 1.23), 123));
echo 
'<br><br>';
var_dump(array_fill_keys(array('test', TRUE, 10, 100), ''));

?>

  10 EXERCISE   

<?php

/* Testing with unexpected argument types */

echo "Testing array_fill_keys() : parameter variations.<br><br>";

$fp = fopen(__FILE__, "r");
$array = array("one", "two");

echo 
"With unusual second arguments:<br>";
var_dump( array_fill_keys($array, $fp) );

fclose($fp);

?>