<svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0,0);"></path></svg><blockquote>
把索引数组转换成按指定键做key的关联数组,没有相应key的元素将被舍弃
/**
* 把索引数组转换成按指定键做key的关联数组,没有相应key的元素将被舍弃
* 主要用做列表转化
* @param array $array
* @param $indexName string 对应的键名,值内容应为string,或可以转为string的类型
* @return array
*/
public static function indexArrayToRelatedArray(array $array,$indexName)
{
$data = [];
foreach ($array as $key => $value) {
if (!is_numeric($key)) {
continue;
}
$index = '';
if (is_array($value) && isset($value[$indexName]) && !empty($value[$indexName])) {
$index = $value[$indexName];
} elseif (is_object($value) && isset($value->$indexName) && !empty($value->$indexName)) {
$index = $value->$indexName;
}
// 重复的将被后面覆盖
if (!empty($index)) {
$data[$index] = $value;
}
}
return $data;
}
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|