使用关联数组方法获取所有数组值时出错

时间:2013-08-28 07:40:33

标签: php arrays codeigniter

现在我很难将所有值放在关联数组中。 因为我只能从查询中获取最后一个值。不是所有的价值观。我无法发现错误。 这是我的代码:

$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
                                  LEFT JOIN cscart_order_data
                                  ON cscart_orders.order_id = cscart_order_data.order_id
                                  WHERE cscart_order_data.type = 'I'
                                  AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
                                  ";

       $resultGetSerialize = $this->db->query($sqlGetSerializedValues);

       echo "<pre>";
       $var_data = array();

       foreach($resultGetSerialize->result_array() as $row1){

            $var_data[] = array(
                            'id' => $row1['order_id'],
                            'total' => $row1['total'],
                            'status' => $row1['status'],
                            'data' => unserialize($row1['data_serialize']),
                            'date' => $row1['date']
                          );            

       }

       $range = array();

       foreach($var_data as $data){

                $id = $data['id'];
                $total = $data['total'];
                $cost = $data['data']['cost'];
                $var_date = $data['date'];
                $status => $data['status'];

                $date_var = date('Y-m-d H:i:s',$var_date);

                $grand_total = $total + $cost;
                $cost_ratio = ($cost/$grand_total) * 100;
                $paid_ratio = ($total/$grand_total) * 100;

                $range[] = $cost_ratio;

                $test = array(
                    'id' => $data['id'],
                    'ratio' => $cost_ratio,
                    'status' => $status 
                );

            }

      echo "</table>";

      print_r($test); //this will return the last index from my array

那是我的问题,我希望你能帮助我。感谢。

2 个答案:

答案 0 :(得分:2)

$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_orders.total AS total, cscart_order_data.data AS data_serialize, cscart_orders.timestamp AS date, cscart_orders.status AS status FROM cscart_orders
                                  LEFT JOIN cscart_order_data
                                  ON cscart_orders.order_id = cscart_order_data.order_id
                                  WHERE cscart_order_data.type = 'I'
                                  AND cscart_orders.timestamp BETWEEN UNIX_TIMESTAMP('2011-01-01 00:00:00') AND UNIX_TIMESTAMP('2013-01-31 23:59:59') limit 10
                                  ";

       $resultGetSerialize = $this->db->query($sqlGetSerializedValues);

       echo "<pre>";
       $var_data = array();

       foreach($resultGetSerialize->result_array() as $row1){

            $var_data[] = array(
                            'id' => $row1['order_id'],
                            'total' => $row1['total'],
                            'status' => $row1['status'],
                            'data' => unserialize($row1['data_serialize']),
                            'date' => $row1['date']
                          );            

       }

       $range = array();
       $test = array();
       foreach($var_data as $data){

                $id = $data['id'];
                $total = $data['total'];
                $cost = $data['data']['cost'];
                $var_date = $data['date'];
                $status => $data['status'];

                $date_var = date('Y-m-d H:i:s',$var_date);

                $grand_total = $total + $cost;
                $cost_ratio = ($cost/$grand_total) * 100;
                $paid_ratio = ($total/$grand_total) * 100;

                $range[] = $cost_ratio;

                $test[] = array(
                    'id' => $data['id'],
                    'ratio' => $cost_ratio,
                    'status' => $status 
                );

            }

      echo "</table>";

      print_r($test); //this will return the last index from my array

答案 1 :(得分:1)

这段代码正在加载标量值

 $test = array(
                'id' => $data['id'],
                'ratio' => $cost_ratio,
                'status' => $status 
            );

应该是

 $test[] = array(
                'id' => $data['id'],
                'ratio' => $cost_ratio,
                'status' => $status 
            );