4

I have two arrays:

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

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. 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.

//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?

Thanks

2 Answers 2

1

You can use the array_key_exists to check if array 2 is missing anything: 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
Sign up to request clarification or add additional context in comments.

2 Comments

I'll try that, but any idea why array_keys wouldn't work the way I am using it?
@pruitigoe array_keys returns a array of the keys found for the value passed. See the Docs
1

The second argument to array_keys is attempting to match the values and not the keys of $formElementPairs.

2 Comments

Look at this example (2nd one) at w3schools:<br><?php $a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog"); print_r(array_keys($a,"Dog")); ?> <br>w3schools.com/php/func_array_keys.asp
Look at this example (2nd one) at w3schools:<?php $a=array("a"=>"Horse","b"=>"Cat","c"=>"Dog"); print_r(array_keys($a,"Dog")); ?>The result will be Array ( [0] => c) Isn't that getting a match result by checking the values of a key? w3school

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.