简体   繁体   English

PHP array_keys-我在做什么错?

[英]PHP array_keys - what am I doing wrong?

I have two arrays: 我有两个数组:

$fieldNames: $ fieldNames:

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: $ formElementPairs:

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

)

I'm trying to check to see if array 2 is missing any of the 'fieldName' keys from array 1 and then add them to array 2 with null entries. 我试图检查数组2是否缺少数组1中的任何“ fieldName”键,然后将它们添加到具有空条目的数组2中。 The following code works in that I am getting the "fieldName" values from the first array (id, adminId, schoolId, etc.) but when I go to check them against the second array using array_keys, my resulting array always has a count of 0. Should also mention I am stuck using PHP4 on this project. 以下代码的工作方式是,我从第一个数组(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));
}

Am I misunderstanding array_keys and/or is there a more elegant way to do this in PHP4? 我是否误解了array_keys和/或在PHP4中是否有更优雅的方法来做到这一点?

Thanks 谢谢

You can use the array_key_exists to check if array 2 is missing anything: array_key_exists 您可以使用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

array_keys的第二个参数试图匹配值,而不是$formElementPairs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM