I'm wondering if anyone has a recursive solution to converting an array to a string.
Here's what I mean:
An array $args
that has the following contents:
Array
(
[0] => $hello
[1] => 411px
[Jeeves] => Array
(
[compiling] => 1
)
)
Result after calling arr_to_string($args)
:
array($hello,"411px", "Jeeves" => array("compiling" => 1));
Note: It recognizes the $ sign in front and therefore does not add quotes. It does the same for numbers.
Anyone have any solution or can point me in the right direction?
Thanks! Matt Mueller
The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.
function convert_multi_array($array) { foreach($array as $value) { if(count($value) > 1) { $array = implode("~", $value); } $array = implode("&", $value); } print_r($array); } $arr = array(array("blue", "red", "green"), array("one", "three", "twenty")); convert_multi_array($arr);
Multidimensional PHP Array to String Maybe in case you need to convert a multidimensional array into a string, you may want to use the print_r() function. This is also called “array with keys”. By adding “true” as its second parameter, all the contents of the array will be cast into the string. <?
Looks like you are after
var_export
— Outputs or returns a parsable string representation of a variableThat won't give you $hello though, because $hello cannot be in an array. It's always just the value of the variable, not the variable name. If you want '$hello', put it into single quotes when inserting it to the array, e.g. insert it as a string, not as a variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With