【问题标题】:PHP foreach gets stripslashes() expects parameter 1PHP foreach 获取 stripslashes() 需要参数 1
【发布时间】:2017-03-06 08:27:23
【问题描述】:
array(1) {
  [0]=>
  array(11) {
    ["deductionspayment"]=>
    array(3) {
      [0]=>
      array(3) {
        ["amount"]=>
        string(4) "1.03"
        ["year"]=>
        string(4) "2017"
        ["month"]=>
        string(5) "March"
      }
      [1]=>
      array(3) {
        ["amount"]=>
        string(4) "1.03"
        ["year"]=>
        string(4) "2017"
        ["month"]=>
        string(5) "April"
      }
      [2]=>
      array(3) {
        ["amount"]=>
        string(4) "1.03"
        ["year"]=>
        string(4) "2017"
        ["month"]=>
        string(3) "May"
      }
    }
    ["deductionsname"]=>
    string(3) "SSS"
    ["deductionstart"]=>
    string(10) "2017-03-08"
    ["deductionsend"]=>
    string(10) "2017-03-14"
    ["deductionsamount"]=>
    string(4) "3.09"
    ["deductionsyears"]=>
    string(1) "3"
    ["deductionsdate"]=>
    string(10) "2017-03-18"
    ["deductionschedule"]=>
    string(1) "3"
    ["deductionstype"]=>
    string(1) "1"
    ["deductionsprincipalamount"]=>
    string(1) "3"
    ["deductionsinterest"]=>
    string(1) "3"
  }
}
<br />
<b>Warning</b>:  stripslashes() expects parameter 1 to be string, array given in 
<b>Warning</b>:  Invalid argument supplied for foreach() in 
{"error":false,"message":"Update Successful.!"}

这是我的 $jsondeduction 的 var_dump,我正在 foreach 中使用它

foreach ($jsondeduction as $val) {
                $jsondeductionspayments = json_decode(stripslashes($val['deductionspayment']), true);

但我收到错误,我假设错误来自这一行: $jsondeductionspayments = json_decode(stripslashes($val['deductionspayment']), true);

我应该如何格式化这些数据以允许stripslashes

【问题讨论】:

  • 第一个问题是——你为什么还要使用stripslashes
  • 在有斜线的情况下删除斜线?
  • 错误本身说你应该传递字符串而不是数组(数组)

标签: php stripslashes


【解决方案1】:

stripslashes

stripslashes — 取消引用带引号的 string

string stripslashes ( string $str )

stripslashes 在发送数组$val['deductionspayment'] 时需要一个字符串参数,该数组不是字符串变量

【讨论】:

    【解决方案2】:

    试试这个:

    foreach ($jsondeduction as $val) {
                    $jsondeductionspayments = stripslashes(json_decode($val['deductionspayment']), true);
    

    【讨论】:

      【解决方案3】:

      Stripslashes 作用于字符串。您提供了一个数组 $val['deductionspayment']。

      string stripslashes ( string $str );

      【讨论】:

        【解决方案4】:

        由于 stripslashs 数组而产生错误。

        $jsondeduction_decode=json_decode($jsondeduction)
        $jsondeduction_stripslash=unstrip_array($jsondeduction_decode['deductionspayment']);
        
             function unstrip_array($array){
                foreach($array as &$val){
                    if(is_array($val)){
                        $val = unstrip_array($val);
                    }else{
                        $val = stripslashes($val);
                    }
                }
            return $array;
            }
        

        【讨论】:

          猜你喜欢
          • 2020-03-09
          • 2016-03-29
          • 2021-12-10
          • 1970-01-01
          • 1970-01-01
          • 2020-06-15
          • 2015-10-16
          • 2016-06-07
          • 2023-03-16
          相关资源
          最近更新 更多