我有这个数组
$array = array(
"one" => "bar",
"two" => "21",
"three" => "22",
"four" => "25",
"five" => array(
"xxxxxxxxxxxxxxxxxx" => array(
"ar1" => "food",
"ar2" => "dr",
"ar3" => "ch",
"ar4" => "ju"
),
"yyyyyyyyyyyyyyyyyyyy" => array(
"ar1" => "food",
"ar2" => "dr",
"ar3" => "ch",
"ar4" => "ju"
)),
"six" => "et",
"seven" => "op",
"eight" => "hjs",
"nine" => array(
"1" => array(
"ar5" => "food",
"ar87" => "dr",
"ar21" => "ch",
"ar443" => "ju"
),
"2" => array(
"73" => "food",
"82" => "dr",
"90" => "ch",
"2123" => "ju"
)),
"ten" => "bar",
"eleven" => "bar",
"twelve" => "bar"
);
我想确保添加的每个新数组都必须有一个唯一的数组键。为此,我首先获取其他人的所有键,即$array['nine']
在我的情况下,然后运行一个函数,直到找到一个唯一键。
添加一个新数组$array['nine']
并获取所有键nine
$keyedd = array_keys($array['nine']);
$appendThis[''.unique_array_key($keyedd).''] = array(
"73" => "nitrous",
"82" => "oxide",
"90" => "laughing",
"2123" => "gas"
);
$array['nine'] = $array['nine'] + $appendThis;
这是功能
function unique_array_key($the_array){
$r = rand();
if (array_key_exists("$r", $the_array)) {
unique_array_key($the_array);
}
else if(!array_key_exists("$r", $the_array)) {
echo $r;
}
}
一旦我运行这个
[nine] => Array
(
[1] => Array
(
[ar5] => food
[ar87] => dr
[ar21] => ch
[ar443] => ju
)
[2] => Array
(
[73] => food
[82] => dr
[90] => ch
[2123] => ju
)
[] => Array
(
[73] => nitrous
[82] => oxide
[90] => laughing
[2123] => gas
)
)
新添加的数组没有数组键。但是,unique_array_key($keyedd);
它本身似乎工作正常。为什么函数没有添加唯一键?