我有一个post操作的数组
$country = $_POST['country']; //The number of countries differ according to the user selection
$c = count($country);
输出:'
Array ( [0] => England,69,93 [1] => Australia,79,84 [2] => Greece,89,73 [3] => Germany,59,73 )`
我将不得不把它分解成一个多维数组,如:
> Array ( [0] => Array ( [0] => England [1] => 69 [2] => 93)
> [1] => Array ( [0] => Australia [1] => 79 [2] => 84)
> [2] => Array ( [0] => Greece [1] => 89 [2] => 73)
> [3] => Array ( [0] => Germany [1] => 59 [2] => 73))
如何在PHP中实现
I tried
$r = array();
foreach($country as &$r){
$r = explode(",", $r);
//for($i = 0; $i < count($country); $i++){
//for($j = 0; $j < count($r); $j++){
//$array[$i][$j] = $r;
//}
//}
}
echo '<br>';
print_r($r);
for循环也不起作用,因此将其注释掉,但如果需要,将其作为一个选项。
print函数现在只打印数组的1。不太确定我做错了什么。任何帮助都是感激的。谢谢你
你就快到了:
$r = array();
foreach($country as $country_item){
$r[] = explode(",", $country_item);
}
echo '<br>';
print_r($r);
以上应该可以运行。
如果你的国家在每个数组中都是唯一的,那对你来说可能更好:
$r = array();
foreach($country as $country_item){
$temp_array = explode(",", $country_item);
$r[$temp_array[0]] = array($temp_array[1], $temp_array[2]);
}
echo '<br>';
print_r($r);
这会给你如下的输出:
> Array ( [England] => Array ( [0] => 69 [1] => 93)
> [Australia] => Array ( [0] => 79 [1] => 84)
> [Greece] => Array ( [0] => 89 [1] => 73)
> [Germany] => Array ( [0] => 59 [1] => 73))
因此,这意味着您可以访问如下国家/地区的数据:
$r[$country_name];
try this
for($i=0;$i<count($country);$i++)
{
$country1[$i] = explode(",", $country[$i]);
}
您正在用循环中的$r重写您的$r主数组-这是解决方案-始终除以变量:
$output = array();
foreach($country as $c){
$parts = explode(',',$c);
$output[] = $parts;
}
print_r($output);