PHP's array_map including keys

前端 未结 18 2699
抹茶落季
抹茶落季 2020-11-30 19:31

Is there a way of doing something like this:

$test_array = array(\"first_key\" => \"first_value\", 
                    \"second_key\" => \"second_valu         


        
相关标签:
18条回答
  • 2020-11-30 20:10

    With PHP5.3 or later:

    $test_array = array("first_key" => "first_value", 
                        "second_key" => "second_value");
    
    var_dump(
        array_map(
            function($key) use ($test_array) { return "$key loves ${test_array[$key]}"; },
            array_keys($test_array)
        )
    );
    
    0 讨论(0)
  • 2020-11-30 20:10

    Look here! There is a trivial solution!

    function array_map2(callable $f, array $a)
    {
        return array_map($f, array_keys($a), $a);
    }
    

    As stated in the question, array_map already has exactly the functionality required. The other answers here seriously overcomplicate things: array_walk is not functional.

    Usage

    Exactly as you would expect from your example:

    $test_array = array("first_key" => "first_value", 
                        "second_key" => "second_value");
    
    var_dump(array_map2(function($a, $b) { return "$a loves $b"; }, $test_array));
    
    0 讨论(0)
  • 2020-11-30 20:14

    This is how I've implemented this in my project.

    function array_map_associative(callable $callback, $array) {
        /* map original array keys, and call $callable with $key and value of $key from original array. */
        return array_map(function($key) use ($callback, $array){
            return $callback($key, $array[$key]);
        }, array_keys($array));
    }
    
    0 讨论(0)
  • 2020-11-30 20:15

    I always like the javascript variant of array map. The most simple version of it would be:

    /**
     * @param  array    $array
     * @param  callable $callback
     * @return array
     */
    function arrayMap(array $array, callable $callback)
    {
        $newArray = [];
    
        foreach( $array as $key => $value )
        {
            $newArray[] = call_user_func($callback, $value, $key, $array);
        }
    
        return $newArray;
    }
    

    So now you can just pass it a callback function how to construct the values.

    $testArray = [
        "first_key" => "first_value", 
        "second_key" => "second_value"
    ];
    
    var_dump(
        arrayMap($testArray, function($value, $key) {
            return $key . ' loves ' . $value;
        });
    );
    
    0 讨论(0)
  • 2020-11-30 20:20

    Based on eis's answer, here's what I eventually did in order to avoid messing the original array:

    $test_array = array("first_key" => "first_value",
                        "second_key" => "second_value");
    
    $result_array = array();
    array_walk($test_array, 
               function($a, $b) use (&$result_array) 
               { $result_array[] = "$b loves $a"; }, 
               $result_array);
    var_dump($result_array);
    
    0 讨论(0)
  • 2020-11-30 20:20

    Another way of doing this with(out) preserving keys:

    $test_array = [
        "first_key"     => "first_value",
        "second_key"    => "second_value"
    ];
    
    $f = function($ar) {
        return array_map(
            function($key, $val) {
                return "{$key} - {$val}";
            },
            array_keys($ar),
            $ar
        );
    };
    
    #-- WITHOUT preserving keys
    $res = $f($test_array);
    
    #-- WITH preserving keys
    $res = array_combine(
        array_keys($test_array),
        $f($test_array)
    );
    
    0 讨论(0)
提交回复
热议问题