简体   繁体   中英

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:

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

Error:

PHP Notice 'yii\\base\\ErrorException' with message 'Array to string conversion'

I am trying to import data from json file and being end up with this error.

Thanks in Advance.

UPDATE:

JSON DATA:

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

Var_dump Result:

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).

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函数,第一个参数必须为字符串(引用)。

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