我想创建一个新数组,并将其键设置为另一个数组的值。
开始:
$names = array("don","james","jennifer","paul");
$ages = array("don" => 25, "paul" => 32);
结束:
$name_age_map = array(
"don" => 25,
"james" => null,
"jennifer" => null,
"paul" => 32,
);
这应该如何在 PHP 中完成?这是我迄今为止最好的:
$name_age_map = array();
foreach ($names as $name) {
$name_age_map[$name] = $name_map[$name];
}
理想情况下,我什至不会创建一个新数组,我只会给 $names 中的元素一个年龄值。