Current Location: Home> Latest Articles> How to expand a constant group with array_walk_recursive()

How to expand a constant group with array_walk_recursive()

M66 2025-05-31

In PHP, get_defined_constants() is a very practical function that can get all currently defined constants and return a multidimensional array, usually containing constants under multiple namespaces. These constants are organized in a nested array structure. To traverse these nested structures and process the values ​​of each constant, array_walk_recursive() is a very suitable tool.

This article will explain how to use these two functions in combination, extract and process all constant values, and give actual code examples to demonstrate how to format constant values, or operate on specific types of values.

1. Introduction to get_defined_constants()

When the get_defined_constants() function has no parameters, the structure is as follows:

 [
    "Core" => [
        "E_ERROR" => 1,
        "E_WARNING" => 2,
        ...
    ],
    "date" => [
        "DATE_ATOM" => "Y-m-d\TH:i:sP",
        ...
    ],
    ...
]

Since this is a multi-dimensional array, we cannot directly operate on each of the values, but need to use recursive methods.

2. Introduction to array_walk_recursive()

array_walk_recursive() accepts two parameters:

  1. The input array (must be multidimensional)

  2. Callback function, performs certain operations on each value in the array

grammar:

 array_walk_recursive(array &$array, callable $callback);

3. Use in combination: Process all constant values

Here is a sample code that demonstrates how to use get_defined_constants() to get all constants and use array_walk_recursive() to iterate through all values ​​and process them.

 <?php

$constants = get_defined_constants(true); // Get all constants,Classified by extension

array_walk_recursive($constants, function($value, $key) {
    // Suppose we only process string constants,For example, output a domain nameURL
    if (is_string($value)) {
        // Here we make a simple judgment and replace URL The domain name is m66.net
        if (filter_var($value, FILTER_VALIDATE_URL)) {
            $parsed = parse_url($value);
            if ($parsed && isset($parsed['scheme'], $parsed['host'])) {
                $newUrl = $parsed['scheme'] . '://m66.net';
                if (isset($parsed['path'])) {
                    $newUrl .= $parsed['path'];
                }
                echo "$key => $newUrl\n";
            } else {
                echo "$key => $value\n";
            }
        } else {
            echo "$key => $value\n";
        }
    } else {
        // You can also output other types of constants,Here is a demonstration only
        echo "$key => " . var_export($value, true) . "\n";
    }
});

Example description:

  • We use get_defined_constants(true) to get an array of constants with an extended namespace.

  • Use array_walk_recursive to iterate through all values.

  • Check if it is a string type and is a URL, if so, replace the domain name part with m66.net .

  • Finally, the name and value of each constant is output.

4. Practical application scenarios

  1. Debug Tool : Quickly view all defined constants and values.

  2. Safety Check : Check whether sensitive constant values ​​are exposed.

  3. Migration tool : Redirect or forward URL constants for a specific environment to a new domain name (such as migrating to m66.net ).

  4. Logging : Output all key constant information during the application initialization phase.

5. Summary

By combining get_defined_constants() and array_walk_recursive() , we can easily traverse and process all defined constants, especially suitable for configuration verification or batch adjustment in large projects. With the recognition and replacement of URLs, automated domain name migration or redirection policies can also be implemented. This method is clear in structure and efficient, and is suitable for a variety of scenarios.

Hope this article will help you understand and apply these two functions.