PHP array_keys - what am I doing wrong?

I have two arrays:

$ field names:

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' 

)

I am trying to check if in array 2 there are any 'fieldName' keys from array 1, and then add them to array 2 with zero values. The following code works in that I get the "fieldName" values ​​from the first array (id, adminId, schoolId, etc.), but when I go to check them on the second array using array_keys, there is always a count of 0 in my resulting array It should also be mentioned that I am stuck using PHP4 in this project.

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

Am I really misunderstanding array_keys and / or is there a more elegant way to do this in PHP4?

thanks

+4
source share
2 answers

You can use array_key_exists to check if array 2 is missing: 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 
+1
source

The second argument to array_keys trying to match the values, not the keys of $formElementPairs .

+1
source

All Articles