1

请参阅我用来构建数组的附加示例

foreach($something AS $key => $row)
{

    $output[] = array("name"=>$row["name"], "points"=>$row["points"]);

}

print_r($output);

这是输出:

Array
(
    [0] => Array
        (
            [name] => Mark
            [points] => 1           
        )

    [1] => Array
        (
            [name] => Sara
            [points] => 2
        )

    [2] => Array
        (
            [name] => Jack
            [points] => 3      
        )

)

我想要做的是将$row["points"]移动到下一个数组元素以获得此输出:

Array
(
    [0] => Array
        (
            [name] => Mark
            [points] =>           
        )

    [1] => Array
        (
            [name] => Sara
            [points] => 1
        )

    [2] => Array
        (
            [name] => Jack
            [points] => 2     
        )

)

我不在乎是否有一些数据丢失,或者我的[points] => 3是否进入新数组。我只需要以编程方式将$row["points"]始终移动到下一个元素。我正在使用next()函数但没有成功,也使用$key+1,我确信我不能用它来实现结果。

在我构建数组时是否可以将其置于顶部,或者我是否被迫稍后使用单独的函数移动元素?换句话说,你会怎么做?

4

3 回答 3

1
Try This:
$output = array();
$something = array(
    '0' => array(
        'name'   => 'Mark',
        'points' => 1,
    ),
    '1' => array(
        'name'   => 'Sara',
        'points' => 2,
    ),
    '2' => array(
        'name'   => 'Jack',
        'points' => 3,
    )
);
$point = '';
foreach($something AS $key => $row)
{
    $output[$key] = array("name"=>$row["name"], "points"=>$point);
    $point = $row['points'];
}

echo '<pre>';
print_r($output);
于 2013-09-18T09:57:10.853 回答
0

试试看是否有帮助:

foreach($something AS $key => $row)
{

    $output[$key]['name']       = $row["name"];
    $output[$key+1]['points']   = $row["points"];
}

print_r($output);
于 2013-09-18T09:46:56.337 回答
0

采用:

foreach($array as $key => $each)
{
    $array[0]['points'] = "";
    $array[$key]['name'] = $each["name"];
    if(isset($array[$key+1])) $array[$key+1]['points'] = $each["points"];
}

print_r($array);
于 2013-09-18T09:58:01.507 回答