I would turn it into CSV form, like so:
$string_version = implode(',', $original_array)
You can turn it back by doing:
$destination_array = explode(',', $string_version)
Answer from Teekin on Stack OverflowI would turn it into CSV form, like so:
$string_version = implode(',', $original_array)
You can turn it back by doing:
$destination_array = explode(',', $string_version)
I would turn it into a json object, with the added benefit of keeping the keys if you are using an associative array:
$stringRepresentation= json_encode($arr);
Warning: Array to string conversion in Variable method
Why does PHP convert integer-string array keys to integer?
Javascript Array Is Getting Converted To A String in PHP File?
Array to string conversion error when running seeder
I have an issue I know how to solve but I am curios to why I need to make this extra step to make it work.
I have the following code:
foreach ($routes as $key => $value) {
$router->$value["method"]($value["path"], "something");
}
the value inside the array $value["method"] is a string that maps to the method names on the class Router
If I run this code I get the error:Warning: Array to string conversion
If I change the code to the following, it works:
foreach ($routes as $key => $value) {
$method = $value["method"];
$router->$method($value["path"], "something");
}The solution is to extract the value from the array before using it as a variable method. It seems that the PHP interpreter doesn't retrieve the value from the array when it's used directly within a variable method, resulting in an 'array to string' error. Extracting the value first resolves this issue.
anyone could clarify?
Example: https://3v4l.org/RmAXf
It is clearly documented - I guess I just never paid close enough attention before: https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax
But I am wondering if anybody knows why? Was this an intentional decision?