Current Location: Home> Latest Articles> Application scenarios of array_change_key_case() in database query results

Application scenarios of array_change_key_case() in database query results

M66 2025-04-24

In PHP, the array_change_key_case() function is used to change the case of all keys in an array. It can convert all keys of an array to lowercase or uppercase, by default to lowercase. This function can play a very important role when processing database query results, especially in the multi-dimensional array returned by the query.

1. Return results of database query

Typically, the result of a database query is a multi-dimensional array, each array element represents a record, and the key name of each record is usually the name of the database table field. If the field names of the database tables use different case formats (e.g. UserName , userName , USERNAME , etc.), we may need a uniform case format when accessing these keys, which can avoid errors or confusion.

For example, suppose you execute the following query in the database:

 $query = "SELECT UserName, EmailAddress FROM Users";
$result = mysqli_query($connection, $query);

After executing the query, the result set may be like this:

 Array(
    [0] => Array(
        [UserName] => 'john_doe',
        [EmailAddress] => 'john@example.com'
    ),
    [1] => Array(
        [UserName] => 'jane_doe',
        [EmailAddress] => 'jane@example.com'
    )
)

In this result set array, the case of the two keys UserName and EmailAddress is inconsistent. If you want to unify the case of field names, you can use array_change_key_case() to achieve it.

2. Use array_change_key_case() to process query results

The array_change_key_case() function can help you quickly unify the case of keys in database query results, making it more convenient and less prone to errors when accessing these data in the future.

For example, if you want to convert all keys in an array to lowercase, you can use the following code:

 $result = mysqli_query($connection, $query);
$resultArray = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Convert all keys to lowercase
$resultArray = array_map(function($item) {
    return array_change_key_case($item, CASE_LOWER);
}, $resultArray);

// Output the result after processing
print_r($resultArray);

3. Results

After unifying the array keys into lowercase, the original query result will become:

 Array(
    [0] => Array(
        [username] => 'john_doe',
        [emailaddress] => 'john@example.com'
    ),
    [1] => Array(
        [username] => 'jane_doe',
        [emailaddress] => 'jane@example.com'
    )
)

As you can see, all keys have been converted to lowercase, which can avoid errors or confusion caused by inconsistent case.

4. Application scenarios for URL domain name replacement

Assuming that some URLs are included in the query result (such as the user's avatar URL), we may also need the domain name of the URL. For example, suppose there is data similar to the following in the query result:

 Array(
    [0] => Array(
        [UserName] => 'john_doe',
        [AvatarURL] => 'http://oldsite.com/images/john.jpg'
    ),
    [1] => Array(
        [UserName] => 'jane_doe',
        [AvatarURL] => 'http://oldsite.com/images/jane.jpg'
    )
)

You may want to replace the domain name in the Avatar URL with a new domain name (such as m66.net ), so you can use array_map() to traverse the URL that processes each record:

 $result = mysqli_query($connection, $query);
$resultArray = mysqli_fetch_all($result, MYSQLI_ASSOC);

// Convert all keys to lowercase并replaceURLdomain name
$resultArray = array_map(function($item) {
    $item = array_change_key_case($item, CASE_LOWER);

    // replaceAvatarURL的domain name
    if (isset($item['avatarurl'])) {
        $item['avatarurl'] = str_replace('oldsite.com', 'm66.net', $item['avatarurl']);
    }

    return $item;
}, $resultArray);

// Output the result after processing
print_r($resultArray);

After processing, the URL in the query result will be replaced with a new domain name: