php自主常用函数

来源:互联网 发布:rhel7关闭linux防火墙 编辑:程序博客网 时间:2024/05/17 01:59
/** * Returns the values of a specified column in an array. * The input array should be multidimensional or an array of objects. * * For example, * * ```php * $array = [ *     ['id' => '123', 'data' => 'abc'], *     ['id' => '345', 'data' => 'def'], * ]; * $result = ArrayHelper::getColumn($array, 'id'); * // the result is: ['123', '345'] * * // using anonymous function * $result = ArrayHelper::getColumn($array, function ($element) { *     return $element['id']; * }); * ``` * * @param array $array * @param string|\Closure $name * @param bool $keepKeys whether to maintain the array keys. If false, the resulting array * will be re-indexed with integers. * @return array the list of column values */public static function getColumn($array, $name, $keepKeys = true){    $result = [];    if ($keepKeys) {        foreach ($array as $k => $element) {            $result[$k] = static::getValue($element, $name);        }    } else {        foreach ($array as $element) {            $result[] = static::getValue($element, $name);        }    }    return $result;}