PHP Array to String equivalent

2020-02-06 02:50发布

问题:

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

回答1:

Looks like you are after

  • var_export — Outputs or returns a parsable string representation of a variable

That 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.