PHP's array_map including keys

前端 未结 18 2763
抹茶落季
抹茶落季 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:02

    This is probably the shortest and easiest to reason about:

    $states = array('az' => 'Arizona', 'al' => 'Alabama');
    
    array_map(function ($short, $long) {
        return array(
            'short' => $short,
            'long'  => $long
        );
    }, array_keys($states), $states);
    
    // produces:
    array(
         array('short' => 'az', 'long' => 'Arizona'), 
         array('short' => 'al', 'long' => 'Alabama')
    )
    

提交回复
热议问题