Heim  >  Artikel  >  Backend-Entwicklung  >  php变换数组格式

php变换数组格式

WBOY
WBOYOriginal
2016-06-06 20:37:36957Durchsuche

如何将此数组

<code> [0] => Array
    (
        [三月外] => Array
            (
                [id] => 1
                [age] => 22
                [birthday] => 10月04日
                [username] => 胡迪
                [distance_birthday_time] => 214
            )

    )

[1] => Array
    (
        [三月外] => Array
            (
                [id] => 6
                [age] => 24
                [birthday] => 12月04日
                [username] => 发给
                [distance_birthday_time] => 275
            )

    )
</code>

变成

<code>[0] => Array
    (
        [三月外] => Array
            (
                [0]=>Array
                    (
                        [id] => 1
                        [age] => 22
                        [birthday] => 10月04日
                        [username] => 胡迪
                        [distance_birthday_time] => 214
                    )
                [1]=>Array
                    (
                        [id] => 6
                        [age] => 24
                        [birthday] => 12月04日
                        [username] => 发给
                        [distance_birthday_time] => 275
                    )
            )
    )
</code>

回复内容:

如何将此数组

<code> [0] => Array
    (
        [三月外] => Array
            (
                [id] => 1
                [age] => 22
                [birthday] => 10月04日
                [username] => 胡迪
                [distance_birthday_time] => 214
            )

    )

[1] => Array
    (
        [三月外] => Array
            (
                [id] => 6
                [age] => 24
                [birthday] => 12月04日
                [username] => 发给
                [distance_birthday_time] => 275
            )

    )
</code>

变成

<code>[0] => Array
    (
        [三月外] => Array
            (
                [0]=>Array
                    (
                        [id] => 1
                        [age] => 22
                        [birthday] => 10月04日
                        [username] => 胡迪
                        [distance_birthday_time] => 214
                    )
                [1]=>Array
                    (
                        [id] => 6
                        [age] => 24
                        [birthday] => 12月04日
                        [username] => 发给
                        [distance_birthday_time] => 275
                    )
            )
    )
</code>

笨点的办法吧,还是去foreach 一一比较去吧,可用key来判断当前的key是否存在,存在的话,直接追加到数据后面,否则就当成新元素增加

你这个就只是这种情况 吗?数组里有没有可能是多个元素的呢?

你是想把相同key的合并吧,

<code>foreach($a as $k=>$v){
    $newArray[$k][] = $v;
}
$myAarray[0] = $newArray;
</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn