Uninitialized array offset

Posted by kimmothy16 on Stack Overflow See other posts from Stack Overflow or by kimmothy16
Published on 2010-05-24T06:59:05Z Indexed on 2010/05/24 10:11 UTC
Read the original article Hit count: 330

Filed under:
|
|

Hey everyone,

I am using PHP to create a form with an array of fields. Basically you can add an unlimited number of 'people' to the form and each person has a first name, last name, and phone number. The form requires that you add a phone number for the first person only. If you leave the phone number field blank on any others, the handler file is supposed to be programmed to use the phone number from the first person.

So, my fields are:

person[] - a hidden field with a value that is this person's primary key.

fname[] - an input field

lname[] - an input field

phone[] - an input field

My form handler looks like this:

$people = $_POST['person']
$counter = 0;

foreach($people as $person):
    if($phone[$counter] == '') {
    // use $phone[0]'s phone number
    } else {
    // use $phone[$counter] number
    }
    $counter = $counter + 1;
endforeach;

PHP doesn't like this though, it is throwing me an

Notice: Uninitialized string offset error.  

I debugged it by running the is_array function on people, fname, lname, and phone and it returns true to being an array. I can also manually echo out $phone[2], etc. and get the correct value. I've also ran is_int on the $counter variable and it returned true, so I'm unsure why this isn't working as intended?

Any help would be great!

© Stack Overflow or respective owner

Related posts about php

Related posts about form