1

I'm trying to accept an unknown number of similarly named POST variables like the following:

foo[bar[0]] = 56 foo[bar[1]] = 43 foo[bar[2]] = ah84 foo[bar[3]] = 92hs 

With the rest of my POST data looking like:

foo[baz] = 1432 foo[expected] = 48hf 

Some requests may have no foobars, but most will have 1, and some with have 2-4.

Ideally I would like to end with an array: array( 56, 43, ah84, 92hs)

Is there a way to loop through the POST variables not knowing the number of them? I can create the array if I know what to expect, but in this case I have no way of telling what will come across.

3 Answers 3

1

Having a look at this example might be a bit of help.

If you know you have an array (with non-sequential indices) and you don't care about the order, the cheapest and easiest fix would be array_values.

In your case, it'd be something like $some_var = array_values($_POST['foo']['bar']).

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

Comments

0

One possible solution:

$vars = array_unique($your_post_data); 

Comments

0

You could get the request header and get the post info from it.

<?php $header = getallheaders(); $postInfo = explode("&", $header[count($header) - 1]); for($i = 0; $i < count($postInfo); $i++) { $a = explode("=", $postInfo[$i]); $postInfo[$i] = $a[1]; } ?> 

Not tested & may not work but I think you'll get the idea.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.