将只有一个值的子数组转换为字符串 [英] convert sub-arrays with only one value to a string

查看:37
本文介绍了将只有一个值的子数组转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,子数组由更多值组成,我希望所有只有一个值的子数组转换为字符串.如何成功扫描多维数组以获得结果?

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] => test@this.site.com
        )

)

这是我希望它结束​​的方式

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] => test@this.site.com

)

推荐答案

无论数组有多深,这都应该有效.

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');
    }
}

然后这样做:

array_walk($array, 'array2string');

这篇关于将只有一个值的子数组转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆