我怎样才能将一个额外的参数传递给array_map回调?在我的例子中,我想将$smsPattern(作为第二个参数,在当前元素之后$featureNames)传递给array_map具有$getLimit闭包的函数:
$features = $usage->getSubscription()->getUser()->getRoles();
// SMS regular expression in the form of ROLE_SEND_SMS_X
$smsPattern = '/^ROLE_SEND_SMS_(?P<l>\d+)$/i';
// Function to get roles names and X from a role name
$getNames = function($r) { return trim($r->getRole()); };
$getLimit = function($name, $pattern) {
if(preg_match($pattern, $name, $m)) return $m['l'];
};
// Get roles names and their limits ignoring null values with array_filter
$featuresNames = array_map($getNames, $features);
$smsLimits = array_filter(array_map($getLimit, $featureNames, $smsPattern));
Run Code Online (Sandbox Code Playgroud)
有了这段代码,我得到了一个奇怪的警告: …
我习惯了perl的map()函数,其中回调可以分配键和值,从而创建一个关联数组,其中输入是一个平面数组.我知道array_fill_keys()如果您只想创建一个字典样式的哈希,哪个可能很有用,但是如果您不一定希望所有值都相同呢?显然所有事情都可以通过foreach迭代来完成,但是还有其他(可能更优雅)的方法吗?
编辑:添加一个示例来阐明转换.请不要挂断转换,问题是将平面列表转换为哈希值,我们不能假设所有值都是相同的.
$original_array: ('a', 'b', 'c', 'd')
$new_hash: ('a'=>'yes', 'b'=>'no', 'c'=>'yes', 'd'=>'no')
*note: the values in this example are arbitrary, governed by some business logic that is not really relevant to this question. For example, perhaps it's based on the even-oddness of the ordinal value of the key
Run Code Online (Sandbox Code Playgroud)
实际示例 因此,使用此处提供的答案,以下是如何解析$ _POST以获取仅列出与给定条件匹配的输入字段的列表.这可能很有用,例如,如果表单中有很多输入字段,但必须同时处理它们中的某些组.
在这种情况下,我有许多输入字段,表示数据库的映射.每个输入字段如下所示:
<input name="field-user_email" value="2" />其中每种类型的字段都以"field-"为前缀.
我们想要做的是,首先,获取实际以"field-"开头的那些输入字段的列表,然后我们要创建一个名为的关联数组$mapped_fields,其中提取的字段名称作为键,实际输入字段的值作为价值.
$mapped_fields = array_reduce( preg_grep( '/field-.+/', array_keys( $_POST ) ), function( $hash, $field ){ $hash[substr( $field, 6 )] …Run Code Online (Sandbox Code Playgroud) Clojure有一个数组映射和哈希映射,我无法弄清楚两者之间的区别.任何人都可以用一个例子来解释它们何时会被使用?
我想使用静态方法使用array_map,但我失败了.这是我的代码:
Class Buy {
public function payAllBills() {
$bill_list = OtherClass::getBillList();
return array_map(array(self, 'pay'), $bill_list); // Issue line
}
private static function pay($bill) {
// Some stuff
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
PHP给了我错误:
Use of undefined constant self - assumed 'self'
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
return array_map('self::makeBean()', $model_list);
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
你知道如何使用静态方法使用array_map吗?
我已经读过:在PHP 5.2中可以将一个方法用作array_map函数吗?但这个问题是关于标准方法,而不是静力学.
为什么我的示例代码导致第一个字符串仍然有尾随空格?
$a=array('test_data_1 ','test_data_2');
array_walk($a, 'trim');
array_map('trim', $a);
foreach($a AS $b){
var_dump($b);
}
Run Code Online (Sandbox Code Playgroud)
string(12)"test_data_1"string(11)"test_data_2"
我有一个用于位掩码的32位整数和一个32位值的数组.如何只从数组中获取哪些索引对应于位掩码中非零位的位置?
例如,假设位掩码是49152,二进制是1100000000000000.因此,我必须从数组中获取索引为14和15的元素的值.
有人可以向我解释为什么你不能传递密钥作为参考?
例如:
if(is_array($where)){
foreach($where as &$key => &$value){
$key = sec($key);
$value = sec($value);
}
unset($key, $value);
}
Run Code Online (Sandbox Code Playgroud)
抛出:
Fatal error: Key element cannot be a reference in linkstest.php on line 2
Run Code Online (Sandbox Code Playgroud)
我可以使用array_map做类似的事情吗?我想要做的就是迭代一个关联数组,并使用我的sec()函数转义键和值.
阵列图很难让我理解:
我已经尝试过很多关于array_map的东西,但是我无法直接对它进行操作.
使用数组映射比使用foreach循环获得任何性能优势吗?
我不喜欢foreach的是我无法直接对数组执行操作,并且必须处理创建临时数组并取消设置它们:
foreach($where as $key => $value){
$where[secure($key)] = secure($value);
}
Run Code Online (Sandbox Code Playgroud)
如果它在键中找到要转义的内容,添加新元素并保持未转义的元素,则可能会失败.
所以我坚持这样的事情?
$temparr = array();
foreach($where as $key => $value){
$temparr[secure($key)] = secure($value);
}
$where = $temparr;
unset($temparr);
Run Code Online (Sandbox Code Playgroud)
任何替代品?
我想在同一个数组上运行3个本机函数:trim,strtoupper和mysql_real_escape_string.可以这样做吗?
试图将数组作为回调传递这样做是行不通的:
$exclude = array_map(array('trim','strtoupper','mysql_real_escape_string'), explode("\n", variable_get('gs_stats_filter', 'googlebot')));
Run Code Online (Sandbox Code Playgroud)
虽然这很好,因为它只使用一个本机函数作为回调:
$exclude = array_map('trim', explode("\n", variable_get('gs_stats_filter', 'googlebot')));
Run Code Online (Sandbox Code Playgroud) 现在,如果检测到列表中的"Everything",则输出变为[""].
预期产出:[]
Copy.names = rule.names.map(function(x) {
if (x.name ==='Everything') {
return '';
} else {
return x.name;
}
});
Run Code Online (Sandbox Code Playgroud) 今天我学习了array_map()PHP的一个特例,在文档中作为旁注提到:
Example#4创建一个数组数组
Run Code Online (Sandbox Code Playgroud)<?php $a = array(1, 2, 3, 4, 5); $b = array("one", "two", "three", "four", "five"); $c = array("uno", "dos", "tres", "cuatro", "cinco"); $d = array_map(null, $a, $b, $c); print_r($d); ?>上面的例子将输出:
Run Code Online (Sandbox Code Playgroud)Array ( [0] => Array ( [0] => 1 [1] => one [2] => uno ) [1] => Array ( [0] => 2 [1] => two [2] => dos ) [2] => Array ( [0] => 3 [1] => three [2] => tres ) [3] …