views:

43

answers:

3

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]

)
+1  A: 

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];}'));
}
Rocket
That doesn't work, the resulting array looks as if it hasn't changed, even the single items still are set as arrays.
Paul
Try looping through the array, and using the `array_walk` on each element.
Rocket
I tried that also and still no joy, very strange.
Paul
Rocket
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
Paul
I'm working on it...
Rocket
Seems I can't use `array_walk` for this, gotta do it manually.
Rocket
So for an array 10 levels deep, i have to place 10 nested for-loops to resolve the issue?
Paul
I'm not sure. I didn't realize the array was more than 2 levels deep. I'm looking for a better way.
Rocket
Look at my other answer. Maybe that works?
Rocket
A: 

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');
Rocket
many thanks rocket! worked perfectly!
Paul
A: 
janmoesen