1

I'm getting a syntax error and I can't figure out why. "Parse error: syntax error, unexpected '{' on line 6"

function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} else (is_array($customFields)) {
$parsed = $customFields;
}

// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}

return $returnField
}
1
  • You’re missing a semicolon in the return statement. Commented Jan 1, 2015 at 8:47

7 Answers 7

1

You forget to put semicolon after $returnField, also use elseif instead of else (else don't need any arguments). Use the code below

function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == false && $customFields != null) {
$parsed = array($customFields);
} elseif (is_array($customFields)) {
$parsed = $customFields;
}

// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}

return $returnField;
}

Hope this helps you

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, dumb mistake on my part.
0

else doesn't take any expression after it. Either remove the expression or use elseif instead.

Comments

0

You have missed semicolon in return

return $returnField;
}

Also Else condition else (is_array($customFields)) format is wrong....else dont take any condition after it...You can use Elseif instead

Comments

0
else (is_array($customFields)) {

Is invalid. Use either else if or remove the condition

Comments

0
function ExtractCustomField($fieldName, $customFields) {
// $customFields might be an object, NULL, or an array.
$parsed = array();
if (is_array($customFields) == true && $customFields != null) {
$parsed = array($customFields);
} else {
$parsed = $customFields;
}

// loop through the fields and find the one we are looking for
$returnField = null;
foreach($field as $customFields) {
if ($field->Name == $fieldName) {
$returnField = $field;
break;
}
}

return $returnField;
}

Comments

0

It should else if(is_array($customFields)) or elseif(is_array($customFields))

Comments

0

I have modified your function, i see there is supposed to be and elseif instead of else and in the foreach loop, the syntax in your code was wrong. I have also assumed that you will be using a single level associative array.

<?php
    function ExtractCustomField($fieldName, $customFields) {
            // $customFields might be an object, NULL, or an array.
            $parsed = array();
            if (is_array($customFields) == false && $customFields != null) {
                $parsed = array($customFields);
            } elseif (is_array($customFields)) {
                $parsed = $customFields;
            }

            // loop through the fields and find the one we are looking for
            $returnField = null;
            foreach($customFields as $field) {
                if ($field == $fieldName) {
                    $returnField = $field;
                    break;
                }
            }
            return $returnField;
        }

        echo ExtractCustomField('name', array('name','emial','mobile','password'));
?>

Comments

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.