php数组中的问题用相同的组ID替换null值

时间:2011-01-19 13:48:45

标签: php arrays pointers

我尝试将同一组中的数组值替换为具有该值的数组 但它无法正常工作,你可以在下面的代码中看到为什么?


function trace($val)
{
    echo "pre";
    print_r($val);
    echo "pre";
}
$rows = array(
                        array('a'=>'33333','b'=>'#3333','group'=>1),
                        array('a'=>'','b'=>'','group'=>1),
                        array('a'=>'','b'=>'','group'=>2),
                        array('a'=>'5555','b'=>'#werwe','group'=>2)
                        );
trace($rows);

$oldGroupId = -1;
        foreach($rows as &$row)
            foreach($row as $column=>$fieldValue)
            {
                    if($row['group']!=$oldGroupId)
                        ${$row['group']}[$column]=0;
                    if( !is_null( $row[$column]  ) )
                        ${$row['group']}[$column]=$row[$column];
                    //@ in this place try to point to my dynamick variable pointer for change if change value frome previuse all value in array cahnge
                    $row[$column] = & ${$row['group']}[$column];
            }

trace($rows);
?>

输出中的值:

Array
(
    [0] => Array
        (
            [a] => 33333
            [b] => #3333
            [group] => 1
        )

    [1] => Array
        (
            [a] => 
            [b] => 
            [group] => 1
        )

    [2] => Array
        (
            [a] => 
            [b] => 
            [group] => 2
        )

    [3] => Array
        (
            [a] => 5555
            [b] => #werwe
            [group] => 2
        )

)
Array
(
    [0] => Array
        (
            [a] => 
            [b] => 
            [group] => 1
        )

    [1] => Array
        (
            [a] => 
            [b] => 
            [group] => 1
        )

    [2] => Array
        (
            [a] => 5555
            [b] => #werwe
            [group] => 2
        )

    [3] => Array
        (
            [a] => 5555
            [b] => #werwe
            [group] => 2
        )

)

预期值:

Array
(
    [0] => Array
        (
            [a] => 33333
            [b] => #3333
            [group] => 1
        )

    [1] => Array
        (
            [a] => 33333
            [b] => #3333
            [group] => 1
        )

    [2] => Array
        (
            [a] => 5555
            [b] => #werwe
            [group] => 2
        )

    [3] => Array
        (
            [a] => 5555
            [b] => #werwe
            [group] => 2
        )

)

更新:

如果这不起作用,请告诉我另一种方法吗?

1 个答案:

答案 0 :(得分:1)

开始
$rows = array(
    array('a'=>'33333','b'=>'#3333','group'=>1),
    array('a'=>'','b'=>'','group'=>1),
    array('a'=>'','b'=>'','group'=>2),
    array('a'=>'5555','b'=>'#werwe','group'=>2)
);

创建一个临时变量来保存每个组的整体信息。

$groups = array();

然后将每个非空值添加到数组中。

foreach ($rows as &$row) {
    if (!isset($groups[$row['group']])) {
        $groups[$row['group']] = array();
    }
    $groups[$row['group']] += array_filter($row);
}

最后,用组的整体信息替换每一行。

foreach ($rows as &$row) {
    $row = $groups[$row['group']];
}

输出:See at Codepad