Organizational Research By

Surprising Reserch Topic

Question:how to Fill an array with values, specifying keys?



asked Sep 13, 2013 in PHP by rajesh
edited Sep 12, 2013
+1 vote
19 views



Related Hot Questions

3 Answers

0 votes

 

$array1 = array( 
    "a" => "first", 
    "b" => "second", 
    "c" => "something", 
    "red" 
); 
 
$array2 = array( 
    "a" => "first", 
    "b" => "something", 
    "letsc" 
); 
 
print_r(array_fill_keys($array1, $array2)); 
?> 
 
The output will be 
Array( 
    [first] => Array( 
        [a] => first, 
        [b] => something, 
        [0] => letsc 
    ), 
    [second] => Array( 
        [a] => first, 
        [b] => something, 
        [0] => letsc 
    ), 
    [something] => Array( 
        [a] => first, 
        [b] => something, 
        [0] => letsc 
    ), 
    [red] => Array( 
        [a] => first, 
        [b] => something, 
        [0] => letsc 
    ) 
)
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

 

$a = array("1"); 
 
var_dump(array_fill_keys($a, "test")); 
?> 
 
array(1) { 
  [1]=> 
  string(4) "test" 
} 
 
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes

 

$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>
The above example will output:
Array
(
    [foo] => banana
    [5] => banana
    [10] => banana
    [bar] => banana
)
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...