Notice: Undefined index: no1 in C:\xampp\htdocs\jobs.php on line 6
Notice: Undefined index: no2 in C:\xampp\htdocs\jobs.php on line 6
我该怎么办?
array_combine()
“ array_combine - 通过使用一个数组对键创建一个数组,并且 另一个价值“
$fruit = array(0 => "Lemon", 1 => "Apple");
$order = array(0 => "no1", 1 => "no2");
$new_array = array_combine($order, $fruit);
print_r($new_array);
//输出:数组([NO1] =>柠檬[NO2] => Apple)
工作示例:Https://3v4l.org/rw71r
要实现您需要使用循环的钥匙并使用它来获得水果,请记住,如果钥匙不存在如上所述,您将收到通知。
$fruit = array(0 => "Lemon", 1 => "Apple");
$order = array(0 => "no1", 1 => "no2");
$new_array = array();
foreach ($order as $key => $index) {
$new_array[$index] = $fruit[$key];
}
print_r($new_array);