关联数组总是只返回4个结果

时间:2015-03-16 14:00:48

标签: php arrays associative-array

您想为以下查询结果创建一个关联数组:

$sql = "select s.*,di.dealsimage,
                  ctm.city,
                  l.location,
                  GROUP_CONCAT(DISTINCT cm.cuisine ORDER BY scr.cuisine_sequence_for_store) AS cui,
                  GROUP_CONCAT(DISTINCT rtm.restaurant_type ORDER BY srr.rest_type_sequence_for_store) AS restauranttype                   
                  from stores s 
                  left join city_master ctm on s.city_id = ctm.city_id 
                  left join locations l on s.location_id = l.location_id
                  left join store_cuisine_relation scr on s.store_id = scr.store_id 
                  left join cuisine_master cm on scr.cuisine_id = cm.cuisine_id 
                  left join store_resttype_relation srr on s.store_id = srr.store_id
                  left join restaurant_type_master rtm on rtm.rest_type_id = srr.rest_type_id
                  left join store_dealcat_relation sdr on s.store_id = sdr.store_id 
                  left join deals_category_master dcm on dcm.deal_cat_id = sdr.deal_cat_id
                  left join deals_image di on di.`store_id` = s.store_id
                  where $condition1 s.is_active = 1 $condition2 group by (s.store_id) order by s.store_id";
                 //echo $sql;exit;
         //echo $sql;exit;        

        $sqlex1 = mysqli_query($db,$sql);

        $custom_count = @mysqli_num_rows($sqlex1); // it prints 28


while($result1 = mysqli_fetch_assoc($sqlex1)){

            $dataArr = array_push_assoc($dataArr, 'store_id', $result1['store_id']);
            $dataArr = array_push_assoc($dataArr, 'store_name', $result1['store_name']);
            $dataArr = array_push_assoc($dataArr, 'store_logo', $result1['store_image_url']);
            $dataArr = array_push_assoc($dataArr, 'deals_image', $result1['dealsimage']);           

        }
        //echo count($dataArr);exit;
        //echo $kl;exit;
        //$result = array_merge_recursive($gpsArr,$dataArr);



function array_push_assoc($array, $key, $value){
            $array[$key][] = $value;
            return $array;
        }

查询返回28结果但是当我尝试回显$dataArr的计数时,它总是打印4。我做错了什么?我怎样才能做到这一点? 提前致谢

2 个答案:

答案 0 :(得分:0)

通过您的功能array_push_assoc()创建包含4个键store_idstore_name, store_logo , and deals_image`的数组,每个键有28行

试试这个:

   dataArr = array();
   while($result1 = mysqli_fetch_assoc($sqlex1))
   {
        $dataArr[] = array('store_id'=>$result1['store_id'],
                    'store_name'=>$result1['store_name'],
                    'store_logo'=>$result1['store_image_url'],
                    'deals_image'=>$result1['dealsimage']);          

    }

这将创建具有28个关联数组的数组

答案 1 :(得分:0)

您的代码应更改为:

while($result1 = mysqli_fetch_assoc($sqlex1))
{
  array_push($dataArr, array ('store_id'=> $result1['store_id'], 'store_name'=> $result1['store_name'], 'store_logo'=> $result1['store_image_url'], 'deals_image'=> $result1['dealsimage']));           

}