- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
"first_value", "second_key" => "second_valu-6ren">
有没有办法做这样的事情:
$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_keys
和array_values
,而是直接传递$test_array
变量?
想要的输出是:
array(2) {
[0]=>
string(27) "first_key loves first_value"
[1]=>
string(29) "second_key loves second_value"
}
最佳答案
不适用于array_map,因为它不处理键。
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"
// }
它确实改变了作为参数给出的数组,所以它不完全是函数式编程(因为你有这样标记的问题)。此外,正如评论中所指出的,这只会更改数组的值,因此键不会是您在问题中指定的键。
如果你愿意,你可以编写一个函数来修复上面的点,如下所示:
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() 访问一级键而不调用 `array_keys()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13036160/
PHP array_keys 如何搜索值? 例子: $array2 = array("xyz", "xyz", "abc", "abc", "xyz", "xyz", "text", "abc", "
这个问题在这里已经有了答案: Deleting an element from an array in PHP (25 个答案) 关闭 8 年前。 我有一个 PHP 例程可以处理表单并将值输出到 C
我有两个数组: $字段名: array( [0] => array( ['fieldName'] =>'id' ['fieldType'] => 'int(11)' ) [1]
array_keys返回数组中部分的或所有的键名 说明 array array_keys ( array $array [, mixed $search_value [, bool $stric
使用此解决方案将数组插入数据库 Insert array into MySQL database with PHP产生 MySql 错误。 我的代码: // Create arrays and val
也许是一个简单的问题,但我无法弄明白..我尝试将数组中的值放入变量中,但它似乎不起作用。 $array = array(0 => 100, "color" => "red"); print_r(arr
我想返回 PHP 数组中的所有键,其中相应的值包含搜索元素。 array_keys如果值与搜索词完全匹配,将起作用,但如果搜索词出现在值中的某处但不完全匹配,则不会起作用。 我怎样才能做到这一点? 最
给定一个如下所示的关联数组, $field_defaults = array( 'id' => 0, 'name' => 'new item', 'desc' => '', 'pare
由于某些奇怪的原因,下面的函数在输入 2 时返回 11,而我希望它返回 1。怎么了? 1, 21 => 1, 3 => 2, 6 => 2, 11 => 2,
我遇到了一个问题: 我有两个如下所示的数组: $array1 = array('1','2','1','3','1'); $array2 = array('1','2','3'); // Un
我想返回一个包含键的数组: Content)): $jarray= json_decode($values[0]->Content); print_r(array_keys($jarray)); ex
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: how to fetch array keys with jQuery? php 内置函数 array_ke
在获取数组的所有键时,哪个性能更好?array_keys 还是 foreach ?我想知道 array_keys 是使用 foreach 循环还是 for 循环来获取键的函数..(因为 foreach
这个问题在这里已经有了答案: Is the order of keys returned from array_keys the same as the order in the input arr
我的代码可以追溯到 2011 年,而且我在代码布局方式的严格标准方面遇到了很多错误。 if (!isset($End)) { $Info['Ca
有没有办法做这样的事情: $test_array = array( "first_key" => "first_value", "second_key" => "second_valu
目前我的“方法”检查 array_key 是否在数组中,我需要补充一点,如果 key 长度为 13 就可以了,但是当它更长时,它必须删除类似的前 5 个数字列表。我的代码根据我的 key 从数据库中搜
PHP 文档指出 default value of array_keys second argument是NULL。 但是,当显式传递 NULL 时,array_keys 似乎无法正常工作。 示例:
假设我想遍历一个数组,或者我从不查看值,或者我在其中设置东西,所以我只想要键。哪个更快: // Set a variable each iteration which is unused. forea
我能够使用 array_keys 和 array_values 为 MySQL 执行 INSERT: $columns = implode(", ", array_keys($cmd_array));
我是一名优秀的程序员,十分优秀!