$ fruit = array(0 =>“柠檬”,1 =>“苹果”); $ order = array(0 =>“ no1”,1 =>“ no2”); $ new_array = array(); foreach($订单为$ index){ $ new_a ...

问题描述 投票:0回答:2
表示:

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 我该怎么办?

您应该使用php中可用的

array_combine()

功能(请参阅PHPDOCS
php arrays associative-array merging-data
2个回答
2
投票

“ 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);


0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.