Php 如何在一维数组中指定键名
我试图将国家代码指定为键,将国家名称指定为值。下面是我的代码,如果有错误请纠正我Php 如何在一维数组中指定键名,php,Php,我试图将国家代码指定为键,将国家名称指定为值。下面是我的代码,如果有错误请纠正我 $return = array(); foreach($result as $key=>$value){ array_splice($return, count($return),0, array($value['country_code']=>$value["name"])); } 看起来这样就足够了: $return = array_column($result, 'name', 'cou
$return = array();
foreach($result as $key=>$value){
array_splice($return, count($return),0, array($value['country_code']=>$value["name"]));
}
看起来这样就足够了:
$return = array_column($result, 'name', 'country_code');
这本质上是一个较短的版本
foreach($result as $key => $value){
$return[$value['country_code'] ] = $value["name"];
}
迭代$result以设置值
foreach($result as $v)
{
$return[$v['country_code']] = $v['name'];
}
这里还有一种方法,使用:
干杯 你把它弄得太复杂了:$return[$value['country_code']]=$value[name];是不是数组_列$result,'name','country_code';谢谢你,兄弟,我想了大约一个小时来解决这个问题。
$modified = array_map(function($result){
return [$result['country_code'] => $result['name']];
}, $resultset);