0

For instance, how to convert the following array:

$array1 = array("value1" => "20", "value2" => 40, array("value3" => 60));

To:

$array1 = array("value1" => "20", "value2" => "40", array("value3" => "60"));
0

3 Answers 3

11
array_walk_recursive($array, function (&$value) { $value = (string)$value; });
Sign up to request clarification or add additional context in comments.

1 Comment

Note: Requires 5.3 for lamba function.
0

You can write recursive function eg.:

function strArr($inp){
    $res=array();
    foreach($inp as $k=>$v){
        if(is_array($v)) $res[$k]=strArr($v);
        else $res[$k]=strval($v);
    }
    return $res;
}
$array1 = array("value1" => "20", "value2" => 40, array("value3" => 60));

$array2 = strArr($array1);

Comments

-1

Converting every element to a string without support for lambda functions:

array_walk_recursive($array, 'strval');

1 Comment

Converting every element to a string without support for lambda functions: array_walk_recursive($array, create_function('&$value', '$value = (string)$value;')); Simply strval as callback won't work.

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.