Home >Backend Development >PHP Tutorial >怎么把数组的值变成健名形成新的数组

怎么把数组的值变成健名形成新的数组

WBOY
WBOYOriginal
2016-06-13 12:10:201343browse

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

本帖最后由 xuexi123abc163 于 2014-11-24 11:40:57 编辑
(
    [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 (<br />  array (<br />    'ak' => '1',<br />    'fs' => '12',<br />    'tss' => 'sd第三方f',<br />  ),<br />  array (<br />    'ak' => '2',<br />    'fs' => '12',<br />    'tss' => '是的发生',<br />  ),<br />  array (<br />    'ak' => '3',<br />    'fs' => '22',<br />    'tss' => '好新玩',<br />  ),<br />  array (<br />    'ak' => '4',<br />    'fs' => '22',<br />    'tss' => '八实得分',<br />  ),<br />  array (<br />    'ak' => '6',<br />    'fs' => '22',<br />    'tss' => '八实得分',<br />  ),<br />);<br />$res = array();<br />foreach($ar as $r) {<br />  $t = $r['fs'];<br />  unset($r['fs']);<br />  $res[$t][] = $r;<br />}<br />print_r($res);
Array<br />(<br />    [12] => Array<br />        (<br />            [0] => Array<br />                (<br />                    [ak] => 1<br />                    [tss] => sd第三方f<br />                )<br /><br />            [1] => Array<br />                (<br />                    [ak] => 2<br />                    [tss] => 是的发生<br />                )<br /><br />        )<br /><br />    [22] => Array<br />        (<br />            [0] => Array<br />                (<br />                    [ak] => 3<br />                    [tss] => 好新玩<br />                )<br /><br />            [1] => Array<br />                (<br />                    [ak] => 4<br />                    [tss] => 八实得分<br />                )<br /><br />            [2] => Array<br />                (<br />                    [ak] => 6<br />                    [tss] => 八实得分<br />                )<br /><br />        )<br /><br />)<br /><br />
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