向数组添加元素会创建一个新数组而不是添加

时间:2015-01-23 07:05:32

标签: php arrays

我一直在尝试向我的数组添加两个元素,这些元素始终是创建的。但在我添加这些元素后,它会创建新元素,而不是添加到现有元素。

<?php
Continues from top....
while($finalRes = mysql_fetch_assoc($excute))
            {
            $tables[] = $finalRes;
            }

            if(mysql_num_rows($excute) != 0){

                $report = new Report();
                $ID = substr($table,11);
                $log = $report->projecteden($ID);
                $a=0;
                $b=0;
                if(property_exists($log, 'counts')){  
                    foreach ($log->counts as $m)      
                    {
                        $UNIQUES = $m->count;                
                        $NON-UNIQUES = $m->ucount;
                        $a += $UNIQUES;
                        $b += $NON-UNIQUES;
                    }
                    $tables['UNIQUES'] = $a;
                    $tables[] = $tables['UNIQUES'];
                    $tables['NON-UNIQUES'] = $b;
                    $tables[] = $tables['NON-UNIQUES'];
                }
            }
            var_dump($tables);
?>

var_dump结果

array (size=3)
  0 => 
    array (size=8)
      'ID' => string '105' (length=3)
      'name' => string 'R158' (length=11)
      'accountname' => string 'DDD' (length=3)
      'accountID' => string '1' (length=1)
      'stat' => string '2' (length=1)
      'total_impr' => string '207' (length=3)
      'min(a.timestamp)' => string '2014-05-16 05:38:01' (length=19)
      'max(a.timestamp)' => string '2015-01-22 05:50:41' (length=19)
  'UNIQUES' => int 45
  'NON-UNIQUES' => int 13

你可以看到UNIQUES和NON-UNIQUES没有对齐,可以帮我把UNIQUES和NON-UNIQUES添加到同一个$ tables数组

3 个答案:

答案 0 :(得分:0)

尝试 -

  while($finalRes = mysql_fetch_assoc($excute)){
        $temp = $finalRes;

        if(mysql_num_rows($excute) != 0){

            $report = new Report();
            $ID = substr($table,11);
            $log = $report->projecteden($ID);
            $a=0;
            $b=0;
            if(property_exists($log, 'counts')){  
                foreach ($log->counts as $m)      
                {
                    $UNIQUES = $m->count;                
                    $NON-UNIQUES = $m->ucount;
                    $a += $UNIQUES;
                    $b += $NON-UNIQUES;
                }
                array_merge($temp ,array('UNIQUES' => $a));
                array_merge($temp ,array('NON-UNIQUES' => $b));
                $tables[] = $temp;
            }
        }
 }
        var_dump($tables);

答案 1 :(得分:0)

像这样分配索引

your code....
$tables[0]['UNIQUES'] = $a;
$tables[0]['NON-UNIQUES'] = $b;
your code ....

如果你想删除0索引,那么

$tables = $tables[0];

答案 2 :(得分:0)

1)您可以创建新的临时数组并向该数组添加元素。 2)使用array_merge将temp数组的元素合并到我们的主数组中,使用key&amp;价值对。 3)使用可以使用下面的代码。它工作正常。

while($finalRes = mysql_fetch_assoc($excute))
{
   $tables[] = $finalRes;
}

if(mysql_num_rows($excute) != 0){
   $report = new Report();
   $ID = substr($table,11);
   $log = $report->projecteden($ID);
   $a=0;
   $b=0;

if(property_exists($log, 'counts')){  
    foreach ($log->counts as $m){
         $UNIQUES = $m->count;             
         $NON-UNIQUES = $m->ucount;
         $a += $UNIQUES;
         $b += $NON-UNIQUES;
    }

    $temp = array('UNIQUES' => $a,'NON-UNIQUES' => $b);
    $tables = array_merge($tables,$temp);
}
}
var_dump($tables);
相关问题