PHP 将混合顺序数组和关联数组转换为关联数组

PHP Transform mixed sequential and associative array to associative array

我只想从 $initial 构建 $goal 数组。有任何想法吗?谢谢

编辑:问题可能是如何区分关联部分和顺序部分。

$intial=[
    "one",
    "two"=>"myTwo",
    "three",
    "four"=>"myFour"
];
$goal=[
    "one"=>null,
    "two"=>"myTwo",
    "three"=>null,
    "four"=>"myFour"
];

'sequential' 部分会有数字键,所以如果你的 'associative' 键总是字符串,你可以用它来区分:

$goal = [];
foreach ($initial as $key => $value) {
    if (is_numeric($key)) {
        $goal[$value] = null;
    } else {
        $goal[$key] = $value;
    }
}
$goal = []; 
 foreach($initial as $key => $val){ 
   if(isset($val){ 
     $goal[$key] = $val; 
   }else{ 
      $goal[$key] = $key; 
   } 
  }