php把一个二维数组中的某个或者某些字段当成 key

/**
 * 把一个二维数组中的某个或者某些字段当成 key
 *
 * @param array  $array 二维数组
 * @param string|array $field 字段,liuchao 修改
 *
 * @return array
 */
function useFieldAsArrayKey($array, $field) {

    $newArr = [];

    if (is_array($field)) {
        foreach ($array as $key => $value) {
            $newKey = '';
            foreach ($field as $f) {
                $newKey .= $value[strval($f)];
            }

            if (!isset($newArr[$newKey])) {
                $newArr[$newKey] = $value;
            }
        }
    } else {
        foreach ($array as $key => $value) {

            $newArr[$value[strval($field)]] = $value;
        }
    }

    return $newArr;
}
Strjson博客交流群QQ群:892570651
Vultr注册就送$50,可开日本/新加坡等机房,【点击注册
Last modification:April 19th, 2019 at 09:45 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment