Raphael Jeger
Raphael Jeger

Reputation: 5232

PHP array from jQuery .map-Function

I have a jQuery-function that passes a list of objects to my server via AJAX:

var pList = $(".post:not(#postF)").map(function() {
  return this.id;
});
    $.ajax({
        type: "POST",
        url: "refresh",
        data: "pList="+pList,
...

on PHP-side, I need to check if a certain needle is in that array, I tried the following:

$pList = $_POST['pList'];

if(in_array('p82', $pList)){
    error_log('as');
}

which does not work, although "p82" is in the array. Maybe the jQuery object is not a real array or not passed to PHP as an array? Thanks for any help.

Upvotes: 0

Views: 709

Answers (1)

billyonecan
billyonecan

Reputation: 20260

Add .get() to the end of your map function:

var pList = $(".post:not(#postF)").map(function() {
  return this.id;
}).get();

Then stringify the pList object:

data: 'pList=' + JSON.stringify(pList)

Decode the json server-side:

json_decode($_POST['pList']);

Upvotes: 1

Related Questions