假设我的数组如下所示:
Array
(
[0] = Charcoal,Natural Gas,Combo
[1] = Charcoal,Propane,Combo
[2] = Charcoal,Propane,Natural Gas,Combo
[3] = Natural Gas
)
我能做些什么让它看起来像这样:
Array
(
[0] = Charcoal,Natural Gas,Combo
)
Array
(
[0] = Charcoal,Propane,Combo
)
Array
(
[0] = Charcoal,Propane,Natural Gas,Combo
)
Array
(
[0] = Natural Gas
)
乍一看,我建议这样做:
$array = Array
(
[0] = Charcoal,Natural Gas,Combo
[1] = Charcoal,Propane,Combo
[2] = Charcoal,Propane,Natural Gas,Combo
[3] = Natural Gas
);
$array2 = array();
foreach ($array as $item) {
$array2[][] = $item;
}
仔细想想,您并没有显示一个新的数组,而是显示了多个数组。这将意味着这样的实现:
foreach ($array as $item) {
print_r($item);
}
您可以使用array_cchunk()函数。
$array = array('Charcoal, Natural gas, Combo' ,'Charcoal,Propane,Combo', 'Charcoal,Propane,Natural Gas, Combo', 'Natural Gas' );
$arr = array_chunk($array,1);
参见演示
## hope this helps ##
<?php
$array = Array('Charcoal, Natural
Gas,Combo','Charcoal,Propane,Combo','Charcoal,Propane,Natural Gas,Combo', 'Natural
Gas');
$d1 = array_slice($array, 0, 1);
$d2 = array_slice($array, 1, 1);
$d3= array_slice($array, 2, 1);
$d4 = array_slice($array, 3, 1);
echo "<pre>";
print_r($d1);
print_r($d2);
print_r($d3);
print_r($d4);
echo "</pre>";
?>