验证PHP数组的键>值 [英] Validate PHP Array Key>Value
问题描述
拥有一个像数组[1]
$ ARR =阵列(
阵列(
ignoreMe=> 123,
checkMe=> 值,
)
阵列(
ignoreMe=> 456,
checkMe=> 值,
)
);
我想检查是否特殊键的内部数组(这里的关键 checkMe
)的值相同。
如果所有的按键具有相同的价值,那么我想删除从内阵列的关键。 (所有阵列)
但是拥有像一个阵列时[2]
$ ARR =阵列(
阵列(
ignoreMe=> 123,
checkMe=> 值,
)
阵列(
ignoreMe=> 456,
checkMe=> 值,
)
阵列(
ignoreMe=> 789
checkMe=> 富,
)
);
所有的键都应该保持不变。
我将如何做到这一点与这个复杂的验证?
(链接 https://github.com/Respect/Validation )
的预期结果[1]
$ ARR =阵列(
阵列(
ignoreMe=> 123,
)
阵列(
ignoreMe=> 456,
)
);
[2],不要触摸
下面是已经尽力了:
$验证= V :: ARR() - >将(V ::键(检查,V ::等于('值')));
好吧,如果你正在运行PHP 5.5+那么你可以使用的 array_column
和组合 array_unique
函数从数组删除的项目,如果他们都具有相同的值:
我不知道到底是什么这样的函数将被调用,所以我只是把它叫做 myFunc的
...
函数myFunc的(数组$改编,$键)
{
//使用一键获取所有的值
$值= array_column($改编,$键); //删除所有重复
$ =独特array_unique($值); //如果仅仅是一项留下那么就意味着
//所有的值是相同的,所以继续
//使用修改它...
如果(计数($唯一)=== 1){ //去了每个阵列...
的foreach($改编为$ X =>&安培; $值){ //和取消的关键
未设置(价值$ [$关键]);
}
}
//返回数组
返回$ ARR;
}
例如:
$ ARR1 =阵列(
阵列(ignoreMe=>中123,checkMe=>中值),
阵列(ignoreMe=>中456,checkMe=>中值),
);
$ ARR2 =阵列(
阵列(ignoreMe=>中123,checkMe=>中值),
阵列(ignoreMe=>中456,checkMe=>中值),
阵列(ignoreMe=>中789,checkMe=>中富),
);//该数组中的所有值是相同的,所以它们
//将被删除
后续代码var_dump($ ARR1);
后续代码var_dump(myFunc的($ ARR1,'checkMe'));
呼应'<小时>';//有此数组中的值是不相同
//为别人,所以这个数组将保持不变
后续代码var_dump($ ARR2);
后续代码var_dump(myFunc的($ ARR2,'checkMe'));
Having an array like [1]
$arr = array(
array(
"ignoreMe" => "123",
"checkMe" => "value",
),
array(
"ignoreMe" => "456",
"checkMe" => "value",
),
);
I'd like to check if special keys (here the key checkMe
) of the inner array have the same value.
If all keys have the same value, then I'd like to remove the key from the inner array. (from all arrays)
But when having an array like [2]
$arr = array(
array(
"ignoreMe" => "123",
"checkMe" => "value",
),
array(
"ignoreMe" => "456",
"checkMe" => "value",
),
array(
"ignoreMe" => "789",
"checkMe" => "foo",
),
);
All keys should stay intact.
How would I do this with this complex validator? (Link https://github.com/Respect/Validation)
Expected result of [1] is
$arr = array(
array(
"ignoreMe" => "123",
),
array(
"ignoreMe" => "456",
),
);
[2] should not be touched
Here is what has been tried:
$validator = v::arr()->each(v::key("check", v::equals('value')));
Okay, if you are running PHP 5.5+ then you can use a combination of the array_column
and array_unique
functions to remove the items from the array, if they all have the same value:
I am not sure exactly what such a function would be called, so I just called it myFunc
...
function myFunc(array $arr, $key)
{
// Get all the values using a key
$values = array_column($arr, $key);
// Remove all duplicates
$unique = array_unique($values);
// If there only is one item left then it means
// that all the values are the same, so proceed
// with modifying it...
if (count($unique) === 1) {
// Go over each array...
foreach ($arr as $x => & $value) {
// And unset the key
unset($value[$key]);
}
}
// Return the array
return $arr;
}
Example:
$arr1 = array(
array("ignoreMe" => "123", "checkMe" => "value"),
array("ignoreMe" => "456", "checkMe" => "value"),
);
$arr2 = array(
array("ignoreMe" => "123", "checkMe" => "value"),
array("ignoreMe" => "456", "checkMe" => "value"),
array("ignoreMe" => "789", "checkMe" => "foo"),
);
// All the values in this array are the same, so they
// will be removed
var_dump($arr1);
var_dump(myFunc($arr1, 'checkMe'));
echo '<hr>';
// There is a value in this array that is not the same
// as the others, so this array will be left intact
var_dump($arr2);
var_dump(myFunc($arr2, 'checkMe'));
这篇关于验证PHP数组的键&GT;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!