To better handle the problem of sparse array completion mentioned a couple years ago...
What you really need in this scenario is an empty array containing all the desired keys, and a sparse array containing the keys and values you want overridden. This PHP5 function does that. (The PEAR package PHP_Compat should be able to fill in the gap -- array_combine() -- for a 4.3 install, if necessary.)
<?php
function array_complete(
$keys="", $sparse="" )
{
if(!is_array($sparse))
$sparse=array();
if(!is_array($keys))
return $sparse;
return array_merge(
array_combine( $keys, array_fill( 0,count(
$keys
),""
)
),$sparse );
}
?>
This merges in your sparse array (inserting any additional keys in that array after the ones you've specified), placing its values in the key order you specify, leaving all the other values blank.
Test call: var_dump(array_complete(array("test1", "test2", "test3", "test4", "test5"), array("test3" => "test3", "test1" => "test1", "garbage" => "garbage")));
Result: array(6) {
["test1"]=>
string(5) "test1"
["test2"]=>
string(0) ""
["test3"]=>
string(5) "test3"
["test4"]=>
string(0) ""
["test5"]=>
string(0) ""
["garbage"]=>
string(7) "garbage"
}