我有一个像这样的数组
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;
}
}
答案 0 :(得分: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);
}
}
}
我希望你明白这里发生了什么? 当然,它只适用于你问题中的第一个数组。