1

I have a multidimensional array, the sub-arrays consist of further values, I would like for all sub-arrays that only have one value to be converted into a string. How can I successfully scan through a multidimensional array to get the result?

Below is a small section of the array as it is now.

[1] => Array
(
    [name] => Array
        (
            [0] => Person's name
        )

    [organisation] => Array
        (
            [0] => This is their organisation
            [1] => aka something else
        )

    [address] => Array
        (
            [0] => The street name
            [1] => The town name
        )

    [e-mail] => Array
        (
            [0] => [email protected]
        )

)

and here is how I would like it to end up

[1] => Array
(
    [name] =>  Person's name

    [organisation] => Array
        (
            [0] => This is their organisation
            [1] => aka something else
        )

    [address] => Array
        (
            [0] => The street name
            [1] => The town name
        )

    [e-mail] => [email protected]

)

3 Answers 3

2

This should do the trick.

function array2string(&$v){
    if(is_array($v) && count($v)==1){
        $v = $v[0];
    }
}
array_walk($array, 'array2string');

Or as a one-liner, since I'm nuts.

array_walk($array, create_function('&$v', 'if(is_array($v) && count($v)==1){$v = $v[0];}'));

EDIT: It looks like that array is an element in a bigger array. You need to put this function inside of a foreach loop.

function array2string(&$v){
    if(is_array($v) && count($v)==1){
        $v = $v[0];
    }
}
foreach($array as &$val){
    array_walk($val, 'array2string');
}

Or using my crazy create_function one-liner.

foreach($array as &$val){
    array_walk($val, create_function('&$v', 'if(is_array($v) && count($v)==1){$v = $v[0];}'));
}
Sign up to request clarification or add additional context in comments.

7 Comments

That doesn't work, the resulting array looks as if it hasn't changed, even the single items still are set as arrays.
Try looping through the array, and using the array_walk on each element.
Ok, I fixed it. The foreach($array as $val) needs to be foreach($array as &$val).
That seems to work with the first few levels, the array I'm currently working with is five levels deep and doesn't work after the third level
Seems I can't use array_walk for this, gotta do it manually.
|
1

This should work no matter how deep the array is.

Put this function in your code:

function array2string(&$v){
    if(is_array($v)){
        if(count($v, COUNT_RECURSIVE) == 1){
            $v = $v[0];
            return;
        }
        array_walk($v, 'array2string');
    }
}

Then do this:

array_walk($array, 'array2string');

Comments

0

This PHP 5.3 code uses a closure. You can use a named function, too, if you want. It specifically checks for strings, and calls itself for nested arrays.

<?php
array_walk($array, $walker = function (&$value, $key) use (&$walker) {
    if (is_array($value)) {
        if (count($value) === 1 && is_string($value[0])) {
            $value = $value[0];
        } else {
            array_walk($value, $walker);
        }       
    }   
});

Comments

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.