0

我正在使用 jQuery 从表单上的隐藏字段中获取值。这些值中的每一个都包含在单引号中,例如'enable'。现在,我正在创建一个数组并将这些值推入其中。我正在使用 javascript 将此数组发送到 PHP 文件。我通过 jQuery AJAX 方法的 url 属性发送这个数组。它的代码如下:

$(document).ready(function() {
  $(document).on('click', '#disable_url', function (e) {    
    e.preventDefault();

    var items = new Array();

    $("input:checked:not(#ckbCheckAll)").each(function() {
      items.push($(this).val());
    });

    var str = $("#user_filter").serialize();    
    var str = decodeURIComponent(str);

    var user_status = new Array();
    for (var i = 0; i < items.length; i++) {
      user_status.push($("#"+items[i]).val());
    }    

    $.ajax({
      type: "POST",
      url: "manage_users.php?op=disable_bulk_users&items="+items+"&options="+str+"&user_status="+user_status,
      dataType: 'json',  
      success: function(data) {
        var redirect_link = data.href;
        window.location.href = redirect_link;
      }          
    });
  })
});

现在 manage_users.php 中的 PHP 代码片段如下:

$request = empty( $_GET ) ? $_POST : $_GET ;
  $op       = $request['op'];
  switch( $op ) {
    case "disable_bulk_users":
   /*I'm using explode here as I'm getting the comma separated list of strings and I want to convert it back into an array*/ 
    $user_ids = explode(',', $request['items']);
    $user_statuses = explode(',', $request['user_status']);
    print_r($user_statuses);
    die;
    break;
  }

如果我打印数组$user_statuses,它会打印如下:

Array
(
    [0] => \'enable\'
    [1] => \'enable\'
    [2] => \'enable\'
    [3] => \'enable\'
    [4] => \'enable\'
    [5] => \'enable\'
    [6] => \'disable\'
    [7] => \'disable\'
    [8] => \'disable\'
    [9] => \'disable\'
    [10] => \'disable\'
    [11] => \'disable\'
    [12] => \'disable\'
    [13] => \'disable\'
    [14] => \'disable\'
    [15] => \'disable\'
    [16] => \'disable\'
    [17] => \'disable\'
    [18] => \'disable\'
    [19] => \'disable\'
)

实际上,我想删除出现在开头的字符串\'出现在每个数组元素末尾的字符串 \'。删除这些字符串后,您能帮我获得干净的数组吗?提前致谢。

4

4 回答 4

1

在php中,

$data = array_map('stripslashes', $data);

或者如果您也想删除引号

$data = array_map('trim', $data, ['\\\''])
于 2013-10-13T05:44:15.823 回答
1

如果您想在客户端执行此操作:

user_status.push($("#"+items[i]).val().replace(/'/g,''));
于 2013-10-13T05:36:16.910 回答
1

尝试这个:

function mytrim(&$item){
 $item = trim($item, "\'");
}
$array = array("\'test\'", "\'test2\'");

array_walk($array, "mytrim");
print_r($array);//Array ( [0] => test [1] => test2 )
于 2013-10-13T05:28:45.437 回答
0

使用字符串替换从字符串中删除“\'”。$status=str_replace("\'",'',$user_statuses);

于 2013-10-13T06:02:58.670 回答