8

I just realized - I have no idea how to POST an empty array from an HTML form to PHP. I can POST arrays of size 1 or larger, but is there any way to post an empty array? I mean, like, you can have:

<form>
  <input type='hidden' name='items[0]' value='Value Item 1' />
 <input type='hidden' name='items[1]' value='Value Item 2' />
</form>

which gives an array of size 2: $_POST['items'] === ["Value Item 1", "Value Item 2"] but is there any way to pass an empty array as the value of a $_POST key? That is, I know I can make $POST['items'] === '', but an empty array ([])?

The specific use case is: I have a one to many form, say like a shopping cart with items. The form is populated by existing "items" from the DB, but can add or delete items, which are in an array of items. The array of items is indexed - if new items are added, they are saved to the DB; if items are deleted, they are removed from the array and deleted from the DB.

All of which works fine - unless all the items are deleted from the form, in which case no "item" key is passed.

I found the perfectly good work-around, which is to put a hidden input "items" = null at the top of the form, so the key is passed even if there are no items, then in PHP if the "items" key is '' I convert it to an empty array - so all works fine. I was just curious in theory if it was possible to POST an empty array. No idea why that is considered inappropriate - hope the explanation helps.

As for why I want to check on the existence of the key - I use the same generic code to process inputs from different forms - so, if the user changes the address on the shopping cart without specifying the items, the items should not be deleted just because there is no "items" key.

To clarify further, I'm not actually building a "Shopping Cart" - I'm building an abstract framework that supports mapping one-to-many POSTs to Eloquent One-To-Many relationships. Any field in a POST with an empty value is deleted from the Object - but any field/key NOT in the POST is NOT deleted or modified in the underlying DB.

Point is, I'm building a generic tool-set - not just trying to get one function to handle one form.

3
  • 1
    just wondering, why? for what purpose case? Commented Oct 2, 2015 at 18:03
  • Your question doesn't make any sense, at least to me. Commented Oct 2, 2015 at 18:06
  • Why not just create an empty array? Commented Oct 2, 2015 at 18:10

1 Answer 1

7

No, you can't (although it seems pointless to know it).

Once you set an array-type form field, it yields as much indexes as there are values to this. In that case, it's an empty string, meaning its value was nothing (''), just because it exists. To PHP, having no explicit value means being something null or empty, which counts as valid value. Nothing means not being declared or defined at all, which is not the case of an empty array, or an array of empty value(s).

<form method="POST" id="test" action="test.php">
	<input type='hidden' name='items[]' />
</form>
<input type="submit" value="Send" form="test">

OUTPUT of print_r($_POST)

Array
(
    [items] => Array
        (
            [0] => 
        )

)

If you want $_POST to be an empty array, just don't send anything via POST method. The superglobal is set at runtime as an empty array. Have fun. :)

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

3 Comments

I checked this as the "answer", since I think it is correct that there is no way to POST an empty array. BUT: The PHP part of the reply is totally incorrect. NULL is different from UNDEFINED and different from an empty string and different from false and different from zero and totally different from an empty array. You can do a "foreach" on an empty array - which does nothing but is not an error. "foreach" with an undefined variable or variable equal to NULL or equal to an empty string is an error.
Which is exactly what I've said: To PHP, being nothing is being something null or empty (notice the or part, where clearly opens the gap to differ null from empty, and even from zero, which I didn't mention but counts as well... each, not being nothing, or undefined). Nothing (or UNDEFINED) means not being at all.. I didn't understand where you've disagreed since it's pretty much the same with more details.
I've read a little more, and it does seems that I've expressed myself poorly, since I didn't metioned values. I'll edit the answer with a more appropriate statement, see if it's better now.

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.