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 ?
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
精彩评论