0

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'
5.1tat
  • 19
  • 1
  • 6

2 Answers2

0

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

Joako-stackoverflow
  • 506
  • 1
  • 6
  • 16
0

to use implode function in php first parameter must be string (reference.)