PHP格式阵列结构

时间:2013-12-02 07:00:18

标签: php arrays

我有一个问题,希望有人可以帮助我

我有像这样的php数组

Array
(
    [0] => Array
        (
            [0] => AJP
            [1] => PR4 125 Enduro / SM
            [2] => 125 c.c.
        )
[1] => Array
    (
        [0] => DUCATI
        [1] => YR 125 Desert
        [2] => 125 c.c.
    )

[2] => Array
    (
        [0] => GAS GAS
        [1] => Endurocross TT 80
        [2] => 80 c.c.
    )

[3] => Array
    (
        [0] => GAS GAS
        [1] => EC 125 Six Days 2T
        [2] => 125 c.c.
    )

[4] => Array
    (
        [0] => GAS GAS
        [1] => EC 125 R
        [2] => 125 c.c.
    )

[5] => Array
    (
        [0] => GAS GAS
        [1] => Enducross EC 125
        [2] => 125 c.c.
    )

[6] => Array
    (
        [0] => GAS GAS
        [1] => Enducross HX 125
        [2] => 125 c.c.
    )

[7] => Array
    (
        [0] => GAS GAS
        [1] => Enducross TT 125
        [2] => 125 c.c.
    )

[8] => Array
    (
        [0] => GAS GAS
        [1] => MC 125
        [2] => 125 c.c.
    )

[9] => Array
    (
        [0] => GAS GAS
        [1] => Enducross EC 200
        [2] => 200 c.c.
    )

[10] => Array
    (
        [0] => GAS GAS
        [1] => EC 200 Six Days 2T
        [2] => 200 c.c.
    )

[11] => Array
    (
        [0] => GAS GAS
        [1] => EC 250 2T / EC 250 E 2T
        [2] => 250 c.c.
    )

[12] => Array
    (
        [0] => GAS GAS
        [1] => EC 250 4T / Six Days 2T
        [2] => 250 c.c.
    )

[13] => Array
    (
        [0] => GAS GAS
        [1] => EC 250 4T R / EC 250 R
        [2] => 250 c.c.
    )

[14] => Array
    (
        [0] => GAS GAS
        [1] => Enducross TT 250
        [2] => 250 c.c.
    )

[15] => Array
    (
        [0] => GAS GAS
        [1] => FSE EC 250 4T
        [2] => 250 c.c.
    )

[16] => Array
    (
        [0] => GAS GAS
        [1] => MC 250
        [2] => 250 c.c.
    )

[17] => Array
    (
        [0] => GAS GAS
        [1] => EC 300 / EC 300 Nanbolin
        [2] => 300 c.c.
    )

[18] => Array
    (
        [0] => GAS GAS
        [1] => EC 300 R
        [2] => 300 c.c.
    )

[19] => Array
    (
        [0] => GAS GAS
        [1] => EC 300 Six Days 2T
        [2] => 300 c.c.
    )

[20] => Array
    (
        [0] => GAS GAS
        [1] => Enducross EC 400 FSE
        [2] => 400 c.c.
    )

[21] => Array
    (
        [0] => GAS GAS
        [1] => SM 450 FSR
        [2] => 450 c.c.
    )

[22] => Array
    (
        [0] => GAS GAS
        [1] => Enducross EC 450 FSE
        [2] => 450 c.c.
    )

[23] => Array
    (
        [0] => GAS GAS
        [1] => EC 450 4T R
        [2] => 450 c.c.
    )

[24] => Array
    (
        [0] => GAS GAS
        [1] => FSE 450 SM
        [2] => 450 c.c.
    )

[25] => Array
    (
        [0] => GAS GAS
        [1] => Pampera 450 4T
        [2] => 450 c.c.
    )

[26] => Array
    (
        [0] => GAS GAS
        [1] => EC 515 FSR 4T
        [2] => 515 c.c.
    )

我想将其格式化为

Array
(
    [AJP] => Array
        (
            [1] => PR4 125 Enduro / SM
            [2] => 125 c.c.
        )

[DUCATI] => Array
    (
        [1] => YR 125 Desert
        [2] => 125 c.c.
    )
[GAS GAS] => Array
    (
        [0] => Array
            (
                [1] => EC 125 Six Days 2T
                [2] => 125 c.c.
            )

        [1] => Array
            (
                [1] =>EC 125 Six Days 2T
                [2] => 125 c.c.
            )

        [2] => Array
            (
                [1] => EC 125 Six Days 2T
                [2] => 125 c.c.
            )
    )

) 因此,品牌名称像AJP,DUCATI或GAS GAS成为关键,它们中的所有模型都变成了多阵列。

4 个答案:

答案 0 :(得分:1)

我没有测试这段代码;在飞行中写,所以可能会有一些错误,但这是一个粗略的近似。假设您的数组名为$ arX:

$arNew = array();
foreach ($arX as $x) {
  $newX = array();
  for ($i=1; $i<count($x); $i++) {
     $newX[] = $x[$i];
  }
  $arNew[$x[0]] = $newX;
}
return $arNew;

注意:这实际上将重新索引从[0]而不是[1]开始的新数组。如果维护新索引的顺序至关重要,则需要修改代码以在数组中使用$ i的值,但重新编号为0似乎更有意义。

答案 1 :(得分:1)

$arrNew = array();
foreach ($arrCurr as $arr) {
  $arrTemp = array();

  for ($i=1; $i < count($arr); $i++) {
     $arrTemp[] = $arr[$i];
  }
  $arrNew[$arr[0]][] = $arrTemp;
}
print_r($arrNew);

答案 2 :(得分:0)

给定$ keys = array('a','b','c',...)和你的数组$ list,然后执行以下操作:

$list = array_combine($keys, array_values($list));

这看起来很简单,但是array_values会生成数组的完整副本,因此您可能会遇到空间问题。我们在这里所做的就是让php为我们做循环,而不是消除循环。我很想做更像的事情:

foreach ($list as $k => $v) {
unset ($list[$k]);

$new_key =  *some logic here*

$list[$new_key] = $v;
}

答案 3 :(得分:0)

PHPFiddle:http://phpfiddle.org/main/code/4mi-ap0

$raw = array(
    array('ajp', 1, 2),
    array('ajp', 4, 5),
    array('ducati', 3, 4)
    );

$prep = array();
foreach($raw as $key => $value) {

    $type = $value[0];

    $prep[$type][] = $raw[$key];

    /*if($type === 'ajp') {
        $prep['ajp'][] = $raw[$key];
    }
    else if($type === 'ducati') {
        $prep['ducati'][] = $raw[$key];
    }*/

}

echo '<pre>';
print_r($prep);
echo '</pre>';

结果:

Array
(
    [ajp] => Array
        (
            [0] => Array
                (
                    [0] => ajp
                    [1] => 1
                    [2] => 2
                )

            [1] => Array
                (
                    [0] => ajp
                    [1] => 4
                    [2] => 5
                )

        )

    [ducati] => Array
        (
            [0] => Array
                (
                    [0] => ducati
                    [1] => 3
                    [2] => 4
                )

        )

)