PHP关于数组的各类处理方法

获取数组中的唯一值

function getUniqueValues($array) {
    return array_unique($array);
}

// 示例用法
$numbers = [1, 2, 3, 2, 1, 4, 5];
$result = getUniqueValues($numbers);
print_r($result);  // 输出结果: Array ( [0] => 1 [1] => 2 [2] => 3 [5] => 4 [6] => 5 )

过滤数组中的空值

function filterEmptyValues($array) {
    return array_filter($array, function($value) {
        return !empty($value);
    });
}

// 示例用法
$data = ['apple', '', 'orange', null, 'banana'];
$result = filterEmptyValues($data);
print_r($result);  // 输出结果: Array ( [0] => apple [2] => orange [4] => banana )

判断数组是否关联数组

function isAssociativeArray($array) {
    return array_keys($array) !== range(0, count($array) - 1);
}

// 示例用法
$assocArray = ['name' => 'John', 'age' => 25];
$result1 = isAssociativeArray($assocArray);  // 返回 true

$indexedArray = [0 => 'apple', 1 => 'banana', 2 => 'orange'];
$result2 = isAssociativeArray($indexedArray);  // 返回 false

连接多个数组

function mergeArrays(...$arrays) {
    return array_merge(...$arrays);
}

// 示例用法
$array1 = [1, 2, 3];
$array2 = ['a', 'b', 'c'];
$result = mergeArrays($array1, $array2);
print_r($result);  // 输出结果: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => a [4] => b [5] => c )

数组按指定键排序

function sortArrayByKey($array, $key, $ascending = true) {
    usort($array, function($a, $b) use ($key, $ascending) {
        if ($a[$key] == $b[$key]) {
            return 0;
        }
        return ($a[$key] < $b[$key]) ? ($ascending ? -1 : 1) : ($ascending ? 1 : -1);
    });
    return $array;
}

// 示例用法
$users = [
    ['name' => 'Alice', 'age' => 25],
    ['name' => 'Bob', 'age' => 30],
    ['name' => 'Charlie', 'age' => 20],
];
$result = sortArrayByKey($users, 'age');
print_r($result);
// 输出结果:
// Array (
//     [0] => Array ( [name] => Charlie [age] => 20 )
//     [1] => Array ( [name] => Alice [age] => 25 )
//     [2] => Array ( [name] => Bob [age] => 30 )
// )

过滤数组中满足条件的元素

function filterArray($array, $callback) {
    return array_filter($array, $callback);
}

// 示例用法
$numbers = [1, 2, 3, 4, 5];
$result = filterArray($numbers, function($value) {
    return $value % 2 === 0;
});
print_r($result);  // 输出结果: Array ( [1] => 2 [3] => 4 )

数组去重

function uniqueArray($array) {
    return array_values(array_unique($array));
}

// 示例用法
$numbers = [1, 2, 3, 2, 1, 4, 5];
$result = uniqueArray($numbers);
print_r($result);  // 输出结果: Array ( [0] => 1 [1] => 2 [2] => 3 [4] => 4 [6] => 5 )

键值交换

function swapArrayKeysValues($array) {
    return array_flip($array);
}

// 示例用法
$data = ['name' => 'John', 'age' => 25];
$result = swapArrayKeysValues($data);
print_r($result);  // 输出结果: Array ( [John] => name [25] => age )

数组排序

function sortByKey($array, $key, $ascending = true) {
    $direction = $ascending ? SORT_ASC : SORT_DESC;
    array_multisort(array_column($array, $key), $direction, $array);
    return $array;
}

// 示例用法
$users = [
    ['name' => 'Alice', 'age' => 25],
    ['name' => 'Bob', 'age' => 30],
    ['name' => 'Charlie', 'age' => 20],
];
$result = sortByKey($users, 'age');
print_r($result);
// 输出结果:
// Array (
//     [0] => Array ( [name] => Charlie [age] => 20 )
//     [1] => Array ( [name] => Alice [age] => 25 )
//     [2] => Array ( [name] => Bob [age] => 30 )
// )

你可能感兴趣的:(php,开发语言,排序,去重,数组)