-6

私はこのような配列を持っています

Array
(
    [0] => 125 c.c.
    [1] => Array
        (
            [0] => AK 125 SL
            [1] => 12/13
            [2] => R
            [3] => 1201X
            [4] => L
            [5] => FX22,0
            [6] => 220
            [7] => 58
            [8] => 4
            [9] => 3
            [10] => 8,5
            [11] => Drum
            [12] => -
            [13] => -
            [14] => -
            [15] => -
            [16] => -
            [17] => -
            [18] => -
        )
[2] => Array
    (
        [0] => EVO 125 NE
        [1] => 12/13
        [2] => R
        [3] => 1201X
        [4] => L
        [5] => FX22,0
        [6] => 220
        [7] => 58
        [8] => 4
        [9] => 3
        [10] => 8,5
        [11] => Drum
        [12] => -
        [13] => -
        [14] => -
        [15] => -
        [16] => -
        [17] => -
        [18] => -
    )

[3] => 150 c.c.
[4] => Array
    (
        [0] => EVO 150 NE
        [1] => 12/13
        [2] => R
        [3] => 1201X
        [4] => L
        [5] => FX22,0
        [6] => 220
        [7] => 58
        [8] => 4
        [9] => 3
        [10] => 8,5
        [11] => Drum
        [12] => -
        [13] => -
        [14] => -
        [15] => -
        [16] => -
        [17] => -
        [18] => -
    )

[5] => Array
    (
        [0] => RXT 150
        [1] => 12/13
        [2] => R
        [3] => 1201X
        [4] => L
        [5] => FX22,0
        [6] => 220
        [7] => 58
        [8] => 4
        [9] => 3
        [10] => 8,5
        [11] => Drum
        [12] => -
        [13] => -
        [14] => -
        [15] => -
        [16] => -
        [17] => -
        [18] => -
    )

)

最終的には次のようになるようにフォーマットしたいと思います。

Array
(
    [125 c.c.] => Array
        (
         [0]=>Array
              (
                [0] => AK 125 SL
                [1] => 12/13
                [2] => R
                [3] => 1201X
                [4] => L
                [5] => FX22,0
                [6] => 220
                [7] => 58
                [8] => 4
                [9] => 3
                [10] => 8,5
                [11] => Drum
                [12] => -
                [13] => -
                [14] => -
                [15] => -
                [16] => -
                [17] => -
                [18] => -
            )
         [1]=> Array
          (
            [0] => EVO 125 NE
            [1] => 12/13
            [2] => R
            [3] => 1201X
            [4] => L
            [5] => FX22,0
            [6] => 220
            [7] => 58
            [8] => 4
            [9] => 3
            [10] => 8,5
            [11] => Drum
            [12] => -
            [13] => -
            [14] => -
            [15] => -
            [16] => -
            [17] => -
            [18] => -
        )

       )   



    [150 c.c.] => 
        ARRAY
    (
      [0]  => Array
        (
            [0] => EVO 150 NE
            [1] => 12/13
            [2] => R
            [3] => 1201X
            [4] => L
            [5] => FX22,0
            [6] => 220
            [7] => 58
            [8] => 4
            [9] => 3
            [10] => 8,5
            [11] => Drum
            [12] => -
            [13] => -
            [14] => -
            [15] => -
            [16] => -
            [17] => -
            [18] => -
        )

    [1] => Array
        (
            [0] => RXT 150
            [1] => 12/13
            [2] => R
            [3] => 1201X
            [4] => L
            [5] => FX22,0
            [6] => 220
            [7] => 58
            [8] => 4
            [9] => 3
            [10] => 8,5
            [11] => Drum
            [12] => -
            [13] => -
            [14] => -
            [15] => -
            [16] => -
            [17] => -
            [18] => -
        )
   )
)

現在、私はどんな助けも大歓迎だとは思いません。

 foreach($array as  $val) {
                if(!is_array($val))
                {
                    $finalArray[go here as key]=if not then Go here as value to current key;
                }
            }
4

1 に答える 1

1
$currentKey = '';
$newArray = array();
foreach ($array as  $val) {
    if (!is_array($val)) {
         $currentKey = $val;
    } else {
         if (isset($newArray[$currentKey]) && is_array($newArray[$currentKey])) {
             $newArray[$currentKey][] = $val;
         } else {
             $newArray[$currentKey] = array($val);
         }
    }
}

ここで何が起こっているのか理解していただければ幸いです。もちろん、最初の質問のような配列でのみ機能します。

于 2013-09-11T07:06:09.743 に答える