<bdo id='4h3pI'></bdo><ul id='4h3pI'></ul>

    <small id='4h3pI'></small><noframes id='4h3pI'>

    <i id='4h3pI'><tr id='4h3pI'><dt id='4h3pI'><q id='4h3pI'><span id='4h3pI'><b id='4h3pI'><form id='4h3pI'><ins id='4h3pI'></ins><ul id='4h3pI'></ul><sub id='4h3pI'></sub></form><legend id='4h3pI'></legend><bdo id='4h3pI'><pre id='4h3pI'><center id='4h3pI'></center></pre></bdo></b><th id='4h3pI'></th></span></q></dt></tr></i><div id='4h3pI'><tfoot id='4h3pI'></tfoot><dl id='4h3pI'><fieldset id='4h3pI'></fieldset></dl></div>
      <tfoot id='4h3pI'></tfoot><legend id='4h3pI'><style id='4h3pI'><dir id='4h3pI'><q id='4h3pI'></q></dir></style></legend>
    1. PHP 的 array_map 包括键

      时间:2023-08-20
      <tfoot id='uQxlh'></tfoot>

      1. <i id='uQxlh'><tr id='uQxlh'><dt id='uQxlh'><q id='uQxlh'><span id='uQxlh'><b id='uQxlh'><form id='uQxlh'><ins id='uQxlh'></ins><ul id='uQxlh'></ul><sub id='uQxlh'></sub></form><legend id='uQxlh'></legend><bdo id='uQxlh'><pre id='uQxlh'><center id='uQxlh'></center></pre></bdo></b><th id='uQxlh'></th></span></q></dt></tr></i><div id='uQxlh'><tfoot id='uQxlh'></tfoot><dl id='uQxlh'><fieldset id='uQxlh'></fieldset></dl></div>

                <tbody id='uQxlh'></tbody>
            • <legend id='uQxlh'><style id='uQxlh'><dir id='uQxlh'><q id='uQxlh'></q></dir></style></legend>

            • <small id='uQxlh'></small><noframes id='uQxlh'>

              • <bdo id='uQxlh'></bdo><ul id='uQxlh'></ul>

                本文介绍了PHP 的 array_map 包括键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有没有办法做这样的事情:

                Is there a way of doing something like this:

                $test_array = array("first_key" => "first_value", 
                                    "second_key" => "second_value");
                
                var_dump(array_map(function($a, $b) { return "$a loves $b"; }, 
                         array_keys($test_array), 
                         array_values($test_array)));
                

                但是不是调用array_keysarray_values,而是直接传递$test_array变量?

                But instead of calling array_keys and array_values, directly passing the $test_array variable?

                所需的输出是:

                array(2) {
                  [0]=>
                  string(27) "first_key loves first_value"
                  [1]=>
                  string(29) "second_key loves second_value"
                }
                

                推荐答案

                不使用 array_map,因为它不处理键.

                Not with array_map, as it doesn't handle keys.

                array_walk:

                $test_array = array("first_key" => "first_value",
                                    "second_key" => "second_value");
                array_walk($test_array, function(&$a, $b) { $a = "$b loves $a"; });
                var_dump($test_array);
                
                // array(2) {
                //   ["first_key"]=>
                //   string(27) "first_key loves first_value"
                //   ["second_key"]=>
                //   string(29) "second_key loves second_value"
                // }
                

                它确实改变了作为参数给出的数组,所以它不完全是函数式编程(因为你有这样标记的问题).此外,正如评论中所指出的,这只会更改数组的值,因此键不会是您在问题中指定的键.

                It does change the array given as parameter however, so it's not exactly functional programming (as you have the question tagged like that). Also, as pointed out in the comment, this will only change the values of the array, so the keys won't be what you specified in the question.

                如果您愿意,您可以编写一个函数来修复上面的点,如下所示:

                You could write a function that fixes the points above yourself if you wanted to, like this:

                function mymapper($arrayparam, $valuecallback) {
                  $resultarr = array();
                  foreach ($arrayparam as $key => $value) {
                    $resultarr[] = $valuecallback($key, $value);
                  }
                  return $resultarr;
                }
                
                $test_array = array("first_key" => "first_value",
                                    "second_key" => "second_value");
                $new_array = mymapper($test_array, function($a, $b) { return "$a loves $b"; });
                var_dump($new_array);
                
                // array(2) {
                //   [0]=>
                //   string(27) "first_key loves first_value"
                //   [1]=>
                //   string(29) "second_key loves second_value"
                // }
                

                这篇关于PHP 的 array_map 包括键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:PHP - 相对路径“需要" 下一篇:PHP 闭包中的 use 关键字是否通过引用传递?

                相关文章

                <i id='GRNQo'><tr id='GRNQo'><dt id='GRNQo'><q id='GRNQo'><span id='GRNQo'><b id='GRNQo'><form id='GRNQo'><ins id='GRNQo'></ins><ul id='GRNQo'></ul><sub id='GRNQo'></sub></form><legend id='GRNQo'></legend><bdo id='GRNQo'><pre id='GRNQo'><center id='GRNQo'></center></pre></bdo></b><th id='GRNQo'></th></span></q></dt></tr></i><div id='GRNQo'><tfoot id='GRNQo'></tfoot><dl id='GRNQo'><fieldset id='GRNQo'></fieldset></dl></div>
              • <small id='GRNQo'></small><noframes id='GRNQo'>

                1. <tfoot id='GRNQo'></tfoot>

                    <bdo id='GRNQo'></bdo><ul id='GRNQo'></ul>

                    <legend id='GRNQo'><style id='GRNQo'><dir id='GRNQo'><q id='GRNQo'></q></dir></style></legend>