例如
[Nationality_1] string(8)=>Indian [Nationality_5] string(12)=>American [Nationality_12] string(17)=>Japanese
我通过foreach循环获得了这些数组值,但是我想通过索引将这些值放到单个数组中
期望的输出
[Nationality] [0] string(8)=>Indian [1] string(12)=>American[2]string(17)=>Japanese
我已尝试array_values
但输出Null
我尝试了这个但是在循环中为多个订单创建重复的数组请帮助我。谢谢
$Nationality[] = $value;
答案 0 :(得分:1)
一个解决方案是代码:
$nationality = array(
'Nationality_1' => 'Indian',
'Nationality_2' => 'American',
'Nationality_3' => 'Japanese'
);
$temp = array();
foreach ($nationality as $val) {
$temp[] = $val;
}
$nationality = $temp;
print_r($nationality);
unset($temp);
// Array ( [0] => Indian [1] => American [2] => Japanese )
答案 1 :(得分:0)
听起来您可能希望使用array_values
从输入列表中提取所有国籍。
<强> array_values 强>
返回数组的所有值
你用它做什么然后取决于你,但是从你想要的输出看来你似乎想要它们在另一个数组下用#34;国籍&#34;键?
这是一个例子......
$input = array(
'Nationality_1' => 'Indian',
'Nationality_5' => 'American',
'Nationality_12' => 'Japanese'
);
$output = array('Nationality' => array_values($input));
var_dump($output);
/*
array(1) {
["Nationality"]=>
array(3) {
[0]=>
string(6) "Indian"
[1]=>
string(8) "American"
[2]=>
string(8) "Japanese"
}
}
*/
请参阅操作代码:https://eval.in/869938