Is there a function out there to make sure that any given array conforms to a particular structure? What I mean is that is has particular key names, perhaps particular types for values, and whatever nested structure.
是否有一个函数来确保给定的数组符合特定的结构?我的意思是它有特定的键名,可能是值的特定类型,以及任何嵌套结构。
Right now I have a place where I want to make sure that the array getting past has certain keys, a couple holding a certain data type, and one sub-array with particular key names. I've done a lot of run-around because I was passing malformed arrays to it, and finally I'm at the point where I have a bunch of
现在我有一个地方,我想要确保通过的数组有特定的键,一对拥有特定数据类型的夫妇,和一个具有特定键名的子数组。我做了很多遍历因为我给它传递了畸形的数组,最后我得到了一堆
if ( ! isset($arr['key1']) ) { .... }
if ( ! isset($arr['key2']) ) { .... }
if ( ! isset($arr['key3']) ) { .... }
I would have saved a lot of time and consternation if I could have checked that the array conformed to a particular structure beforehand. Ideally something like
如果我能事先检查数组是否符合特定的结构,我就能节省很多时间和精力。理想像
$arrModel = array(
'key1' => NULL ,
'key2' => int ,
'key3' => array(
'key1' => NULL ,
'key2' => NULL ,
),
);
if ( ! validate_array( $arrModel, $arrCandidate ) ) { ... }
So, the question I'm asking is, does this already exists, or do I write this myself?
我的问题是,这个已经存在了吗,还是我自己写的?
6 个解决方案
#1
6
It doesn't exist built in.
它不存在。
Maybe try something like (untested):
也许可以试试(未经测试的):
array_diff(array_merge_recursive($arrCandidate, $arrModel), $arrModel)
#2
10
Convert array to JSON:
将数组转换为JSON:
http://us.php.net/manual/en/function.json-encode.php
http://us.php.net/manual/en/function.json-encode.php
Then check against a JSON Schema:
然后根据JSON模式进行检查:
http://json-schema.org/
http://jsonschemaphpv.sourceforge.net/
http://jsonschemaphpv.sourceforge.net/
#3
1
I know this is sort of an old post, sorry if my answer is not approppriate.
我知道这是一个有点过时的帖子,如果我的回答不被认可,请见谅。
I'm in the process of writing a php package that does exactly what you are asking for, it's called Structure.
我正在编写一个php包,它完全按照您的要求执行,它叫做结构。
What you can do with the package is something like:
你可以用这个包裹做些什么:
$arrayCheck = new \Structure\ArrayS();
$arrayCheck->setFormat(array("profile"=>"array"));
if ($arrayCheck->check($myArray)) {
//...
}
You can check it out here: http://github.com/3nr1c/structure
您可以在这里查看:http://github.com/3nr1c/structure
#4
1
I came across a tool called Matchmaker on GitHub, which looks very comprehensive and has composer support and unit tests:
https://github.com/ptrofimov/matchmaker
在GitHub上,我遇到了一个叫做Matchmaker的工具,它看起来非常全面,并且有composer支持和单元测试:https://github.com/ptrofimov/matchmaker。
You can include it into your project with composer require ptrofimov/matchmaker
.
您可以将它包含到您的项目中,作曲家需要ptrofimov/matchmaker。
#5
0
Create an array defining your structure, and then go through a loop of the array you want to check and compare it with the array structure you defined.
创建一个定义结构的数组,然后对要检查的数组进行循环,并将其与定义的数组结构进行比较。
#6
0
accepted answer make diff based on values, since it's about array structure you dont want to diff values. Insted you should use array_diff_key()
接受的答案基于值生成diff,因为它是关于数组结构的,所以您不希望diff值。您应该使用array_diff_key()
Function alone is not recursive. It will not work out of box on sample array from question.
函数本身不是递归的。它不能从问题的样本数组中跳出来。
#1
6
It doesn't exist built in.
它不存在。
Maybe try something like (untested):
也许可以试试(未经测试的):
array_diff(array_merge_recursive($arrCandidate, $arrModel), $arrModel)
#2
10
Convert array to JSON:
将数组转换为JSON:
http://us.php.net/manual/en/function.json-encode.php
http://us.php.net/manual/en/function.json-encode.php
Then check against a JSON Schema:
然后根据JSON模式进行检查:
http://json-schema.org/
http://jsonschemaphpv.sourceforge.net/
http://jsonschemaphpv.sourceforge.net/
#3
1
I know this is sort of an old post, sorry if my answer is not approppriate.
我知道这是一个有点过时的帖子,如果我的回答不被认可,请见谅。
I'm in the process of writing a php package that does exactly what you are asking for, it's called Structure.
我正在编写一个php包,它完全按照您的要求执行,它叫做结构。
What you can do with the package is something like:
你可以用这个包裹做些什么:
$arrayCheck = new \Structure\ArrayS();
$arrayCheck->setFormat(array("profile"=>"array"));
if ($arrayCheck->check($myArray)) {
//...
}
You can check it out here: http://github.com/3nr1c/structure
您可以在这里查看:http://github.com/3nr1c/structure
#4
1
I came across a tool called Matchmaker on GitHub, which looks very comprehensive and has composer support and unit tests:
https://github.com/ptrofimov/matchmaker
在GitHub上,我遇到了一个叫做Matchmaker的工具,它看起来非常全面,并且有composer支持和单元测试:https://github.com/ptrofimov/matchmaker。
You can include it into your project with composer require ptrofimov/matchmaker
.
您可以将它包含到您的项目中,作曲家需要ptrofimov/matchmaker。
#5
0
Create an array defining your structure, and then go through a loop of the array you want to check and compare it with the array structure you defined.
创建一个定义结构的数组,然后对要检查的数组进行循环,并将其与定义的数组结构进行比较。
#6
0
accepted answer make diff based on values, since it's about array structure you dont want to diff values. Insted you should use array_diff_key()
接受的答案基于值生成diff,因为它是关于数组结构的,所以您不希望diff值。您应该使用array_diff_key()
Function alone is not recursive. It will not work out of box on sample array from question.
函数本身不是递归的。它不能从问题的样本数组中跳出来。