V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yakczh
V2EX  ›  PHP

怎么样将 array 的 key 统一加上前缀 ?

  •  
  •   yakczh · 2014-04-30 11:13:41 +08:00 · 3967 次点击
    这是一个创建于 3647 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如
    $array = array("version" => "1.1",
    "connection" => "close");

    变成

    Array
    (
    [http_version] => 1.1
    [http_connection] => close
    )

    用array_walk

    $test_array = array("version" => "1.1",
    "connection" => "close");
    array_walk($test_array, function($a, &$b) { $b = "http_".$b; });

    var_dump($test_array);

    $test_array没变化


    array_map貌似不处理key
    8 条回复    2014-05-01 23:16:36 +08:00
    oott123
        1
    oott123  
       2014-04-30 11:18:03 +08:00 via Android
    你在回调里这样:
    unset($arr[$b]);
    $arr[new] = $a;
    oott123
        2
    oott123  
       2014-04-30 12:39:11 +08:00 via Android
    @oott123 似乎无视了 php 的闭包,OTL ,楼主 use 吧,或者 foreach …
    bingu
        3
    bingu  
       2014-04-30 14:28:34 +08:00
    手册的array_walk范例差不多就符合你的要求了吧。

    Example #1 array_walk() 例子

    <?php
    $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

    function test_alter(&$item1, $key, $prefix)
    {
    $item1 = "$prefix: $item1";
    }

    function test_print($item2, $key)
    {
    echo "$key. $item2<br />\n";
    }

    echo "Before ...:\n";
    array_walk($fruits, 'test_print');

    array_walk($fruits, 'test_alter', 'fruit');
    echo "... and after:\n";

    array_walk($fruits, 'test_print');
    ?>
    以上例程会输出:

    Before ...:
    d. lemon
    a. orange
    b. banana
    c. apple
    ... and after:
    d. fruit: lemon
    a. fruit: orange
    b. fruit: banana
    c. fruit: apple
    yakczh
        4
    yakczh  
    OP
       2014-04-30 14:35:05 +08:00
    @bingu 是修改key
    yakczh
        5
    yakczh  
    OP
       2014-04-30 14:37:39 +08:00
    @oott123
    array_walk($test_array, function($a, $b) use (&$test_array) {


    unset($test_array[$b]);
    $test_array['http_'.$b]=$a;
    });

    出来是 Array
    (
    [connection] => close
    [http_http_version] => 1.1
    )

    如果是
    array_walk($test_array, function(&$a, $b) use (&$test_array) {
    //unset($test_array[$b]);
    $test_array['http_'.$b]=$a;
    });
    则没反应了
    icanc
        6
    icanc  
       2014-04-30 15:10:03 +08:00
    $test_array = call_user_func(
    function (Closure $callback,$array){
    $results = array();
    foreach ($array as $key => $value){
    list($innerKey, $innerValue) = call_user_func($callback, $key, $value);
    $results[$innerKey] = $innerValue;
    }
    return $results;
    },
    function($k,$v){ return array('http_'.$k,$v);},
    $test_array // 原始数组
    );
    manhere
        7
    manhere  
       2014-04-30 21:12:28 +08:00 via Android
    批量的添加 和 不添加 有什么区别?
    pubby
        8
    pubby  
       2014-05-01 23:16:36 +08:00
    $test_array = array_combine(
    array_map(function($k){ return 'http_'.$k;},array_keys($test_array)),
    array_values($test_array)
    );

    var_dump($test_array);
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3336 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 11:43 · PVG 19:43 · LAX 04:43 · JFK 07:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.