简体   繁体   English

PHP通知“ yii \\ base \\ ErrorException”,消息为“数组到字符串转换”-yii2

[英]PHP Notice 'yii\base\ErrorException' with message 'Array to string conversion' - yii2

My Code: 我的代码:

$transactions->newSet = implode("@s@",$item['pattern']);

Array Value of pattern object from json Being Passed: 来自json的模式对象的数组值被传递:

Array
(
    [0] => /="\something\\//
    [1] => /something\\?t[p]/
)

Error: 错误:

PHP Notice 'yii\\base\\ErrorException' with message 'Array to string conversion' PHP通知“ yii \\ base \\ ErrorException”,消息为“数组到字符串转换”

I am trying to import data from json file and being end up with this error. 我正在尝试从json文件导入数据,并最终出现此错误。

Thanks in Advance. 提前致谢。

UPDATE: 更新:

JSON DATA: JSON数据:

[
  {
    "description": "old_text_id = 2",
    "pattern": [
      "\/something\/",
      "\/something\?t[p]\/"
    ],
    "severity": 0,
    "type": 1,
    "id": 1,
    "name": {
      "subFamily": "fam",
      "variant": "0"
    }
  }]

Var_dump Result: Var_dump结果:

array(2) {
  [0]=>
  string(30) "/something/"
  [1]=>
  string(71) "/something\?t[p]/"
}
PHP Notice 'yii\base\ErrorException' with message 'Array to string conversion'

Ok, the thing is one of the elements is an array itself, since http://php.net/manual/es/function.implode.php function expects each of the elements of the array to be a string (or be able to convert into one). 好的,事情是元素本身就是数组本身,因为http://php.net/manual/es/function.implode.php函数期望数组的每个元素都是字符串(或者能够转换为一个)。

When one of the the elements of the array is an array, it fails. 当数组的元素之一是数组时,它将失败。 And that's when you have an 'Array to String' conversion error. 这就是您遇到“数组到字符串”转换错误的时候。

Basically you can't implode an array of arrays like that. 基本上,您不能内爆这样的数组。

In the next code you may see the issue on the 3rd and 4th line 在下一个代码中,您可能会在第三和第四行看到问题

  $array = [];
  $array[0] = "/something/";
  $array[1][1] = "/something/";
  $array[1][2] = "/something2/";

  $aux = implode("@s@",$array);

  var_dump ($aux);

And here is working: 在这里工作:

  $array = [];
  $array[0] = "/something/";
  $array[1] = "/something/";

  $aux = implode("@s@",$array);

  var_dump ($aux);

Edit for a comment and some crazy english: 编辑评论和一些疯狂的英语:

The problem is not the way you are importing it, the thing you are looking for is "implode multidimensional arrays".You can't print an array of arrays as an string like that. 问题不在于您导入它的方式,而是要寻找的是“内爆多维数组”。您无法将数组数组打印为这样的字符串。 You may look here 你可以看这里

Implode and Explode Multi dimensional arrays 放大和分解多维数组

要在php中使用implode函数,第一个参数必须为字符串(引用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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