我的数组是这样的:
$data = array(
10 => array(
"id" => 50,
"name" => 'chelsea'
),
11 => array(
"id" => 51,
"name" => 'real madrid'
)
);
我希望将数组更改为:
Array
(
[10] => 50
[11] => 51
)
我试着这样:
$data = array_column($data, 'id');
但是,结果是这样的:
Array
(
[0] => 50
[1] => 51
)
如果有人可以解决我的问题吗?
答案 0 :(得分:1)
$data = array(
10 => array(
"id" => 50,
"name" => 'chelsea'
),
11 => array(
"id" => 51,
"name" => 'real madrid'
)
);
foreach($data as $key => $value) {
$new_data[$key] = $value['id'];
}
var_dump($new_data);
希望它有所帮助!
答案 1 :(得分:-1)
您必须使用新信息声明并加载新数组
$new_array = array();
foreach ($data as $key => $value) {
$new_array[$key] = $value['id'];
}
print_r($new_array); //display newly loaded data