0

I'm sending a POST array to a controller. The array looks like such:

Array
(
[event] => Array
    (
        [0] => Array
            (
                ['publishStart'] => 2013-12-10
                ['eventStart'] => 2014-05-05
                ['eventEnd'] => 2014-05-10
                ['timeStart'] => 
                ['timeEnd'] => 
                ['location_id'] => 1
                ['id'] => 65774
            )

There are a few of these blocks, i.e. [event][1]. [event][2], etc. I'm attempting to run a foreach loop on $_POST['event'] and can confirm that on each iteration, $event contains the following:

Array
(
    ['publishStart'] => 2013-12-10
    ['eventStart'] => 2014-05-05
    ['eventEnd'] => 2014-05-10
    ['timeStart'] => 
    ['timeEnd'] => 
    ['location_id'] => 1
    ['id'] => 65774
)

Now, the problem. You can see in both above arrays there is a key called "id" with a corresponding value. Yet the following code returns a string of "undefined index" errors:

foreach ($_POST['event'] as $event)
{   
    echo $event['id'];
    exit();
}

What on earth am I doing wrong?

9
  • just for debug, do a var_dump on $event instead of the echo $event['id'] Commented Apr 29, 2014 at 23:08
  • Sorry if I did not make it clear enough, but both arrays above are nicely-formatted versions of both of your requests, direct output from var_dump. The full var_dump for $event is at pastebin.com/x1YrjdGc nd the full var_dump for $_POST is at pastebin.com/fa9zkTWa thanks! Commented Apr 29, 2014 at 23:15
  • 2
    There's probably atleast 1 event-block in your $_POST-array without an id-field. Commented Apr 29, 2014 at 23:15
  • Crazy thing though is that when I try to display a counter to show me how many times it's gone through the loop, it always stops after the first iteration. Does that matter? Commented Apr 29, 2014 at 23:16
  • Do you var_dump / print_r exactly in front of the loop? Commented Apr 29, 2014 at 23:17

1 Answer 1

2

Your array indices seem to include the single-quotes.

Try

echo $event["'id'"];
Sign up to request clarification or add additional context in comments.

1 Comment

Ugh, thank you so much! Elsewhere in the code before the variables got passed to POST the single-quotes were being escaped for some reason. I really appreciate it!

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.