简体   繁体   中英

explode/implode array in PHP

Not sure why I am having this problem. I have used this same code on a previous project with no problems.

I'm generating an array using checkboxes in JavaScript. I can successfully $.post the array to PHP, but I keep receiving the following error:

: explode() expects parameter 2 to be string, array given in on line :explode()期望参数2为字符串,第行的给出的数组

: implode(): Invalid arguments passed in on line :implode():第行的传递的参数无效

It repeats the same error about 4 times, as I have 4 different arrays I'm sending over.

Starting with the JavaScript:

$('#updateRecords').on('click', function(e)
{ 
  e.preventDefault();
  $('#updateForm input').val('');

  var checkcontainer = [];
  var checkorder = [];
  var checktrucker = [];
  var checkconsignee = [];

  $(".checkEdit:checked").each(function(){
    checkcontainer.push($(this).data("checkcontainer"));
    checkorder.push($(this).data("checkorder"));
    checktrucker.push($(this).data("checktrucker")); 
    checkconsignee.push($(this).data("checkconsignee")); 
  });

  console.log(checkcontainer);

  $.post('process/updateRecord.php', {checkcontainer:checkcontainer,
  checkorder:checkorder, checktrucker:checktrucker, checkconsignee:checkconsignee}, 
  function(data)
  {
    console.log(data);
  }); 
});

When I console out the variable 'checkcontainer', I see the following:

["FSCU7122545", "TGHU6235458", "TCNU6900047"]

Over in PHP, the code looks like this:

<?php
if(isset($_POST['checkcontainer']))
{
  $checkcontainer = $_POST['checkcontainer'];
  $checkorder = $_POST['checkorder'];
  $checktrucker = $_POST['checktrucker'];
  $checkconsignee = $_POST['checkconsignee'];

  $containerSplit = explode(",", $checkcontainer);
  $containers = "'" . implode("', '", $containerSplit) . "'";
  $orderSplit = explode(",", $checkorder);
  $orders = "'" . implode("', '", $orderSplit) . "'";
  $truckerSplit = explode(",", $checktrucker);
  $truckers = "'" . implode("', '", $truckerSplit) . "'";      
  $consigneeSplit = explode(",", $checkconsignee);
  $consignees = "'" . implode("', '", $consigneeSplit) . "'";

  echo $containers;
}
?>

As stated, I've used this same code in a previous project. Why am I receiving the above error?

You actually don't need the explode() calls before your implode() ones because the data you send are arrays (Your variables in your js are arrays). So all your $_POST variables are arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM