Home  >  Article  >  Backend Development  >  如何把数组的值变成健名形成新的数组。

如何把数组的值变成健名形成新的数组。

WBOY
WBOYOriginal
2016-06-23 13:44:42973browse


(
    [0] => Array
        (
            [ak] => 1
            [fs] => 12
            [tss] => sd第三方f
        )

    [1] => Array
        (
            [ak] => 2
            [fs] => 12
            [tss] => 是的发生
        )

    [2] => Array
        (
            [ak] => 3
            [fs] => 22
            [tss] => 好新玩
        )

    [3] => Array
        (
            [ak] => 4
            [fs] => 22
            [tss] => 八实得分
        )
    [4] => Array
        (
            [ak] => 6
            [fs] => 22
            [tss] => 八实得分
        )
)

上面的数组是$abc
我下奶想形成新的数组把fs的值作为键名,然后把fs的值相同的,形成新的数组,。。

数组如下:

(
    [12] => Array
        (
     [0] => Array
         (
             [ak] => 2
             [tss] => 是的发生
         )
                  [1] => Array
         (
             [ak] => 2
             [tss] => 是的发生
        )
        )

    [22] => Array
        (
            ...............
        )
)





回复讨论(解决方案)

$ar = array (  array (    'ak' => '1',    'fs' => '12',    'tss' => 'sd第三方f',  ),  array (    'ak' => '2',    'fs' => '12',    'tss' => '是的发生',  ),  array (    'ak' => '3',    'fs' => '22',    'tss' => '好新玩',  ),  array (    'ak' => '4',    'fs' => '22',    'tss' => '八实得分',  ),  array (    'ak' => '6',    'fs' => '22',    'tss' => '八实得分',  ),);$res = array();foreach($ar as $r) {  $t = $r['fs'];  unset($r['fs']);  $res[$t][] = $r;}print_r($res);
Array(    [12] => Array        (            [0] => Array                (                    [ak] => 1                    [tss] => sd第三方f                )            [1] => Array                (                    [ak] => 2                    [tss] => 是的发生                )        )    [22] => Array        (            [0] => Array                (                    [ak] => 3                    [tss] => 好新玩                )            [1] => Array                (                    [ak] => 4                    [tss] => 八实得分                )            [2] => Array                (                    [ak] => 6                    [tss] => 八实得分                )        ))

感受就是牛B。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn