2

Given

$.post('dealWithStuff.php',{'fullArray':['hello','goodby'],'emptyArray':[]});

dealWithStuff.php receives

['fullArray'=>['hello','goodby'] ]

but not

['fullArray'=>['hello','goodby'], 'emptyArray'=>[] ]

How do I send the emptyArray to the server?

user1032531
  • 24,767
  • 68
  • 217
  • 387
  • What's the code you use to print that debugging text out? – Blender Jul 25 '14 at 01:23
  • @Blender PHP? Do you feel I shouldn't see these results? – user1032531 Jul 25 '14 at 01:31
  • 2
    Whoops, I thought you were sending this as JSON. You can't send an empty array as a POST parameter because it would be indistinguishable from `['']`. Encode your object as JSON and decode it with PHP to preserve the structure. – Blender Jul 25 '14 at 01:34

3 Answers3

1

Use null which is legal value in JSON.

Just for the sake of curiosity, why would you send an empty array to server? You just could check server-side if array is set. (isset() for php)

1

I'll give this a crack.

Just before we start, This would be a good question for you to read over.

You can't send an empty array because it'd look something like this: file.php?arra[].

You'll need to do this when sending:

$.post('dealWithStuff.php',{'fullArray':['hello','goodby'],'emptyArray':['']});

Notice how emptyArray is [''] instead of [] ? This will give you a response that looks like this:

Array
(
    [fullArray] => Array
        (
            [0] => hello
            [1] => goodby
        )

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

)

You'll be able to access it from the backend, you'll just have to remove that child array element from within the emptyArray.

easily done with something like this:

foreach($_POST['emptyArray'] as $i => $derp) {
    unset($_POST['emptyArray'][$i]);
}

Presuming you don't want any items in the emptyArray. After that, you'll get a return that looks like this:

Array
(
    [fullArray] => Array
        (
            [0] => hello
            [1] => goodby
        )

    [emptyArray] => Array
        (
        )
)
Community
  • 1
  • 1
Darren
  • 13,050
  • 4
  • 41
  • 79
0

It doesn't really make sense to send an empty array. Think about what the browser would send to the server. The data that is sent for your post would be:

fullArray[]:hello
fullArray[]:goodby

Which would be translated in PHP as:

$_POST['fullArray'] = array(0 => "hello", 1 => "goodby");

But if you were to send say,

emptyArray[]:

It would be treated as an empty string value and you'll get something like:

$_POST['emptyArray'] = array(0 => "");

Which is probably not what you want (and not what jQuery does). Change your back-end if possible to detect the existence of the key.

sahbeewah
  • 2,690
  • 1
  • 12
  • 18
  • Changing the backend doesn't fix it. What I need is for the server to receive the "emptyArray" key, and when no elements are attached, know to do something else. That being said, I do appreciate your answer and will mull over it some more. – user1032531 Jul 25 '14 at 02:12
  • Have a key called ```emptyArrays``` which contains a list of strings representing arrays that are empty, and manipulate/populate it on the front-end upon form-submit. Your other alternative is to send all the data through a single key (maybe ```data```) and send a serialized version of the object (JSON) into your server. Then you can simply ```json_decode``` it. – sahbeewah Jul 25 '14 at 02:31
  • 1
    -1 for "*change your backend to detect the existence*". It is jQuery on the client side that eats the parameter, as you can easily test. – Bergi Jul 25 '14 at 03:13
  • @Bergi Nobody is disputing that? Which means the key no longer exists when the PHP receives the data, telling you that that array was empty. – sahbeewah Jul 25 '14 at 05:32
  • Did you mean "change the backend to detect whether the key is present, and treat it as empty otherwise"? Your current phrasing sounds like the PHP should detect the non-existant key. – Bergi Jul 25 '14 at 12:08