gpt4 book ai didi

PHP array_keys - 我做错了什么?

转载 作者:可可西里 更新时间:2023-10-31 23:34:28 24 4
gpt4 key购买 nike

我有两个数组:

$字段名:

array(
[0] => array(
['fieldName'] =>'id'
['fieldType'] => 'int(11)'
)
[1] =>
['fieldName'] =>'adminID'
['fieldType'] =>'int(11)'
)
[2] =>array(
['fieldName'] =>'schoolID'
['fieldType'] =>'int(11)'
)
[3] => array(
['fieldName'] =>'lessonPlanName'
['fieldType'] =>'varchar(255)'
)
[4] =>array(
['fieldName'] =>'lessonPlanAssignmentDate'
['fieldType'] =>'varchar(255)'
)
[5] =>array(
['fieldName'] =>'lessonPlanDueDate'
['fieldType'] =>'varchar(255)'
)
[6] =>array(
['fieldName'] =>'lessonPlanTopics'
['fieldType'] =>'varchar(255)'
)
[7] =>array(
['fieldName'] =>'lessonPlanDescription'
['fieldType'] =>'text'
)
[8] =>array(
['fieldName'] =>'lessonPlanNotes'
['fieldType'] =>'text'
)
)

$formElementPairs:

array(
['lessonPlanName'] =>'Test'
['lessonPlanAssignedDate'] =>'05/11/2011'
['lessonPlanDueDate'] =>'05/11/2011'
['lessonPlanTopics'] => 1
['lessonPlanDescription'] =>'test'

)

我正在尝试检查数组 2 是否缺少数组 1 中的任何“fieldName”键,然后将它们添加到包含空条目的数组 2 中。以下代码的工作原理是我从第一个数组(id、adminId、schoolId 等)中获取“fieldName”值,但是当我使用 array_keys 将它们与第二个数组进行比较时,我得到的数组总是有一个计数0. 还应该提到我在这个项目上一直使用 PHP4。

//merge arrays
for($fn=0; $fn<count($fieldNames); $fn++) {
$thisFieldName = $fieldNames[$fn]['fieldName'];
$fieldCheckArray = array_keys($formElementPairs, $thisFieldName);
//$firephp->fb(count($fieldCheckArray));
}

我是否误解了 array_keys 和/或在 PHP4 中是否有更优雅的方式来做到这一点?

谢谢

最佳答案

您可以使用 array_key_exists 检查数组 2 是否缺少任何内容:array_key_exists

$missing = array();
for($fn=0; $fn<count($fieldNames); $fn++) {
$thisFieldName = $fieldNames[$fn]['fieldName'];
if(!array_key_exists($thisFieldName, $formElementPairs)) {
$missing[] = $thisFieldName;
}
}
//do something with $missing

关于PHP array_keys - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5954594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com