-3

I have the following array:

array('16 HOURS','13.3 HOURS','10.6 HOURS AGO','8 HOURS AGO','5.3 HOURS AGO','2.6 HOURS AGO','CURRENT')

I want to insert empty cells ('') between every string until the desired length is reached. I have tried various loops, for i etc but always end up with white spaces behind CURRENT or before 16 HOURS, thanks in advance.

4

2 回答 2

1

尝试这个。

$array = array('16 HOURS','13.3 HOURS','10.6 HOURS AGO','8 HOURS AGO','5.3 HOURS AGO','2.6 HOURS AGO','CURRENT');

$i = 1;

foreach($array as $val) {
  $tempArray[] = $val;
  if($i < count($array)) {
    $tempArray[] = '';
  }
  $i++;
}

print_r($tempArray);

结果

大批
(
    [0] => 16 小时
    [1] =>
    [2] => 13.3 小时
    [3] =>
    [4] => 10.6 小时前
    [5] =>
    [6] => 8 小时前
    [7] =>
    [8] => 5.3 小时前
    [9] =>
    [10] => 2.6 小时前
    [11] =>
    [12] => 当前
)
于 2013-01-18T15:31:35.470 回答
0
$myArray = array(
    '16 HOURS',
    '13.3 HOURS',
    '10.6 HOURS AGO',
    '8 HOURS AGO',
    '5.3 HOURS AGO',
    '2.6 HOURS AGO',
    'CURRENT'
);


$newArray = array_combine(
        range(0,12,2),
        $myArray
    ) +
    array_fill_keys(
        range(1,12,2),
        ''
    )
;
ksort($newArray);

var_dump($newArray);
于 2013-01-18T15:42:39.473 回答