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'
8
  • not sure what the problem is, can you var_dump the $item['pattern']. Commented Feb 8, 2018 at 13:03
  • I think you might be confusing data types here, between json and array, please rephrase the explanation of this issue, if you please Commented Feb 8, 2018 at 13:04
  • updated please check the post. Commented Feb 8, 2018 at 13:10
  • this is weird i never encountered such thing would love to see someone answer that Commented Feb 8, 2018 at 13:19
  • You are welcome :) Waiting on answers too. Commented Feb 8, 2018 at 13:20

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

But the elements are imported directly from a json file with file_get_contents. I cant Specify them infile! can you help me in this.?
I can help, I hope. Editing the asnwer
Let everyone know it the issue is solved, so everyone can learn from taht in the future
Solved it. The Problem was in Json File the severity field was set to 0 . while the application only supports value Ranging from 1-9.That was causing the problems.
based on the error shown, that does not seems to be the case. But glad it worked
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.