Reputation: 1
I try foreach loop but it keeps giving me "Undefined offset 2".
I tried also isset then I got "Undefined offset 1"
My code:
foreach ($lineArrayResults as $lineArrayResultKey => $lineArrayResult)
{
$currentFormFieldId = $submittedFormFields[$lineArrayResultKey]; // this line gives me error.
if($currentFormFieldId > 0)
{
$newLeadDataArray[$currentFormFieldId] = [
'field_name' => $formFields[$currentFormFieldId],
'field_value' => $lineArrayResult
];
}
}
Upvotes: 0
Views: 377
Reputation: 1596
the error undefined offset means that an array have ran out of bounds. the array size is smaller than the index that you are trying to fetch an object from.
First of all make sure your $submittedFormFields
array contains the key of $lineArrayResultKey
variable, if the array key is dynamically generated then try this code
foreach ($lineArrayResults as $lineArrayResultKey => $lineArrayResult)
{
if(!array_key_exists($lineArrayResultKey, $submittedFormFields)){
continue;
}
$currentFormFieldId = $submittedFormFields[$lineArrayResultKey];
if($currentFormFieldId > 0)
{
$newLeadDataArray[$currentFormFieldId] = [
'field_name' => $formFields[$currentFormFieldId],
'field_value' => $lineArrayResult
];
}
}
Upvotes: 1
Reputation: 812
add @ before $ try this
foreach ($lineArrayResults as $lineArrayResultKey => $lineArrayResult)
{
$currentFormFieldId = @$submittedFormFields[$lineArrayResultKey]; // insert @ before $
if($currentFormFieldId > 0)
{
$newLeadDataArray[$currentFormFieldId] = [
'field_name' => $formFields[$currentFormFieldId],
'field_value' => $lineArrayResult
];
}
}
Upvotes: 0