1

I have an array that by all rights appears to be, well, an array:

var_dump($arr);

Gives this:

array(3) {
  ["query"]=>
  string(47) "select * from `sessions` where `id` = ? limit 1"
  ["bindings"]=>
  array(1) {
    [0]=>
    string(40) "beec3e058d85062452f025e1f2156f2fefdf87ea"
  }
  ["time"]=>
  float(0.94)
}   

but, I cannot manipulate it as an array.

echo implode("\n", $arr);

Produces a stack trace and 'Array to String Conversion' error in my Laravel app. What is the problem, and how can I make it so this "array" will accept operations like implode or echo $arr['query']?

1 Answer 1

1

You can't do implode with this particular array, because it has an array among its values. Should you have an array containing only scalar values, you'd be ok.

$arr ['query'] works since it is a string, there is no conversion needed.

I assume you want a string representation of an array. Why don't you just use:

var_export($arr, true) ?

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

1 Comment

Thanks for this observation. Back in the old days the value of bindings would just be output as "Array" - which I'm still partial to but yeah, the new way is more precise.

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.