【发布时间】:2018-10-07 03:05:15
【问题描述】:
我有以下白名单:
private $conditionalFieldsWhitelist = [
'Basic Fields' => [
'contact_name',
'contact_lastname',
],
'Required Fields' => [
'some_required_field',
]
];
我想再次运行一个如下所示的数组:
$myArray = [
'Basic Fields' => [
[
'field_name' => 'contact_name',
'field_readable' => $this->language->get('First Name'),
'field_type' => 'string',
'field_required' => 'no',
'field_placeholder' => $this->language->get('Type your first name')
], [
'field_name' => 'contact_lastname',
'field_readable' => $this->language->get('Last Name'),
'field_type' => 'string',
'field_required' => 'no',
'field_placeholder' => $this->language->get('Type your last name')
], [
'field_name' => 'contact_email',
'field_readable' => $this->language->get('Email Address'),
'field_type' => 'string',
'field_required' => 'yes',
'field_placeholder' => 'example@domain.com'
], [
'field_name' => 'contact_mobile',
'field_readable' => $this->language->get('Mobile Number'),
'field_type' => 'string',
'field_required' => 'yes',
'field_placeholder' => '+27881234567'
]
],
'Required Fields' => [
[
'field_name' => 'some_required_field',
'field_readable' => $this->language->get('Required Field'),
'field_type' => 'string',
'field_required' => 'no',
],
],
'This Should Be Removed' => [
[
'field_name' => 'not_needed_field',
'field_readable' => $this->language->get('Required Field'),
'field_type' => 'string',
'field_required' => 'no',
]
]
];
显然这是实际数组的淡化版本。
我的代码如下:
public function getConditionalFields()
{
$conditionalFields = $this->formFieldGroupingsViewHelper->getGroupedFields();
foreach ($conditionalFields as $group => $fields) {
if (in_array($group, array_keys($this->conditionalFieldsWhitelist)) === false) {
unset($conditionalFields[$group]);
continue;
}
foreach ($fields as $index => $field) {
if (in_array($field['field_name'], $this->conditionalFieldsWhitelist[$group]) === false) {
unset($conditionalFields[$group][$index]);
continue;
}
}
$conditionalFields[$group] = array_values($conditionalFields[$group]);
}
return $conditionalFields;
}
然而,这似乎不是很干净,并且利用了 PHP 的强大功能。
有没有更简单、更好、更整洁的方法来做到这一点?类似于递归 array_intersect 的东西也适用于数组键。
这是我期望的结果:
[
'Basic Fields' => [
[
'field_name' => 'contact_name',
'field_readable' => $this->language->get('First Name'),
'field_type' => 'string',
'field_required' => 'no',
'field_placeholder' => $this->language->get('Type your first name')
], [
'field_name' => 'contact_lastname',
'field_readable' => $this->language->get('Last Name'),
'field_type' => 'string',
'field_required' => 'no',
'field_placeholder' => $this->language->get('Type your last name')
]
],
'Required Fields' => [
[
'field_name' => 'some_required_field',
'field_readable' => $this->language->get('Required Field'),
'field_type' => 'string',
'field_required' => 'no',
],
]
]
【问题讨论】:
-
您想要的预期结果是什么?也请添加
-
可能是
array_intersect_key?还是您所在地区没有 google? -
可能还有多个要检查的键吗?不仅仅是“基本领域”?
-
@AlivetoDie:编辑问题以添加结果。
-
@u_mulder:感谢您极具洞察力和真正有用的评论。我试过谷歌。
array_intersect_key不适用于多维数组。
标签: php multidimensional-array filtering whitelist array-intersect