我有一个数组,值如下:
$array1 = [
0 => "year",
1 => "month"
];
我对此数组进行循环,并检查其值是否存在于以下数组中。
array:3 [
0 => array:3 [
"name" => "year"
"mandatory" => true
"type" => "integer"
]
1 => array:3 [
"name" => "month"
"mandatory" => true
"type" => "integer"
]
2 => array:3 [
"name" => "id"
"mandatory" => false
"type" => "integer"
]
]
如果存在,那么我必须返回类似这样的数组
array[
"year" => [
"mandatory" => true
"type" => "integer"
],
"month" => [
"mandatory" => true
"type" => "integer"
],
]
这是到目前为止我尝试过的代码。
$keys = array_keys($params);
$fields = $config["fields"];
$finalArr = array();
foreach($keys as $key) {
if(in_array($key, array_column($fields, "name"))) {
$finalArr[$key] = array();
}
$finalArr[$key][] = $fields;
}
但这将返回所有记录。
有人可以帮助我吗?
答案 0 :(得分:1)
遍历您的$fields
并检查$keys
中是否存在字段名称:
foreach ($fields as $field) {
if (in_array($field['name'], $keys)) {
$finalArr[$field['name']] = $field;
}
}