【发布时间】:2015-05-18 16:15:57
【问题描述】:
您好想为以下查询结果创建一个关联数组:
$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。我做错了什么?我怎样才能做到这一点?
提前致谢
【问题讨论】:
-
... 因为
$dataArr由4个子数组组成,分别是store_id、store_name、store_logo和deals_image?只需执行var_dump($dataArr)并亲自查看。 -
@MatteoTassinari 但表中有 28 行,它应该每 28 行产生一次。我希望每行都有键值 - store_id,store_name,store_logo,deals_image。
标签: php arrays associative-array