Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi basically I am storing form field names in an array. This is working fine as I am able to print out the array and see it's contents. The problem I am having is checking those contents and based on them storing new values in a new array.

PHP

$required = array('frm_companyName', 'frm_hrManager', 'frm_lnManager');
$fieldArray = array();
$errorArray = array();

...

foreach ($required as $field)  {
    if (empty($_POST[$field])) {
    $fieldArray[] = $field;
    }
}

...

if (in_array("frm_companyName", $fieldArray)) {
    $errorArray[] = "Company Name";
}

When I print out the $errorArray[] I am returning an empty array (but $fieldArray[] does show the contents. Any Ideas? I know it's bound to be something simple. /Thanks

UPDATE:

I have changed the line

if ( in_array("frm_companyName", $fieldArray)) {
    $errorArray[] = "Company Name";
}

to

if ( in_array("frm_companyName", $fieldArray)) {
    echo " ---- Working ---- ";
}

to just echo out a message but it isn't echoing so there is definitely something wrong with the check i.e. the in_array.

share|improve this question
    
What does var_dump($fieldArray) say? –  Ja͢ck Aug 19 '13 at 2:36
    
@Jack I get: array(1) { [0]=> string(15) "frm_companyName" } –  Mark H Aug 19 '13 at 2:42
    
Your code should work fine. –  Ja͢ck Aug 19 '13 at 2:44
    
@Jack thanks for your help Jack by using the method to var_dump the array you suggested it got me thinking and looking at other parts of the code (over 1000 lines in total) I soon tracked down the problem. Again thanks :) –  Mark H Aug 19 '13 at 14:09

3 Answers 3

I suppose you missed the ! sign like so:

// Check for missing field
if ( ! in_array("frm_companyName", $fieldArray)) {

    $errorArray[] = "Company Name";

}
share|improve this answer
    
Hi I assume the ! checks if the "frm_companyName" is NOT there, it is there, and I want it to check it IS there. I have updated my question. –  Mark H Aug 19 '13 at 2:38

Thanks for the suggestions guys,

I managed to track down the problem to some earlier code that was interfering. I couldn't post it all as there are over 1000 lines.

Thanks again though.

share|improve this answer
if (in_array("frm_companyName", $fieldArray)) {
    $errorArray[] = "Company Name";
}

is the cause of issue. You should not put String into the array directly. Instead you may want to specify the index of the array, for example

$errorArray[0] = "Company Name";

or just push the string into the array.

array_push($errorArray, "Company Name");
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.