duanshaiduhao2471 2011-01-19 13:48
浏览 31
已采纳

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

i try to replace value of arrays in same group with that one has the value but its not work correctly you can see in the code below why?


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);
?>

the value in output:

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
        )

)

the value expected:

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
        )

)

update :

if this is not work please tell me another way for this reason ?

  • 写回答

1条回答 默认 最新

  • doudu7626 2011-01-19 15:19
    关注

    Start with

    $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)
    );
    

    Create a temporary variable to hold the overall info of each group.

    $groups = array();
    

    Then add each non-empty value to the array.

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

    Finally, replace each row with the overall info of the group.

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

    Output: See at Codepad

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,