如何从下面的示例转换多维数组
Array
(
[0] => Array
(
[cf] => Juan
[nf] => 5
)
[1] => Array
(
[cf] => Kyu
[nf] => 10
)
)
使用值作为键和值的简单数组,其中[cf]是键,[nf]是值
Array
(
"Juan"=>"5",
"Kyu"=>"10"
)
答案 0 :(得分:1)
$arr = array(
0 => array(
'cf' => 'Juan',
'nf' => 5
),
1 => array(
'cf' => 'Kyu',
'nf' => 10
)
);
$result = array();
foreach($arr as $key => $value) {
$result[$value['cf']] = $value['nf'];
}
print_r($result);
答案 1 :(得分:0)
你可以这样做..
<?php
$arr = array( 0 => array( "cf" => "Juan", "nf" => 5, ),1 => array( "cf" => "Kyu", "nf" => 10, ),);
foreach ($arr as $arr1)
{
foreach($arr1 as $k=>$v)
{
$arrnew[]=$v; // Adding just the values to the temp array
}
$new_arr[$arrnew[0]]=$arrnew[1]; //Your new array gets those values from the temp array
unset($arrnew); //Deleting your temp array
}
print_r($new_arr); //Printing the results
<强> OUTPUT :
强>
Array
(
[Juan] => 5
[Kyu] => 10
)
答案 2 :(得分:0)
尝试这样:
$a = array(array('cf'=>'joan', 'nf'=>'5'), array('cf'=>'lol', 'nf'=>'55'));
$new = array();
foreach( $a as $k ) {
$new[$k[cf]] = $k[nf];
}
print_r( $new )
答案 3 :(得分:0)
$x=array(
0=>array(
"cf"=>"Juan",
"nf" => 5,
),
1=>array(
"cf"=>"Kyu",
"nf" => 10,
),
);
foreach($x as $k=>$v) $result[$v["cf"]]=$v["nf"];
print_r($result);
答案 4 :(得分:0)
你遍历它,并建立你的数组,如下所示:
$mysimplearray = array();
foreach($originalarray as $id => $innerarray) {
$mysimplearray[$innerarray["cf"]] = $innerarray["nf"];
}
瞧,你的数据现在作为一个简单的1维数组存储在$ simplearray中。
答案 5 :(得分:0)
这里有你的功能:
$array =
array (
0 => Array
(
'cf' => 'Juan',
'nf' => 5
),
1 => Array
(
'cf' => 'Kyu',
'nf' => 10
)
);
$new_array = array();
foreach($array as $value => $new_array_elem) {
$key = $new_array_elem['cf'];
$value = $new_array_elem['nf'];
$new_array["$key"] = $value;
}
print_r($new_array);