Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关联数组始终仅返回4个结果_Php_Arrays_Associative Array - Fatal编程技术网

Php 关联数组始终仅返回4个结果

Php 关联数组始终仅返回4个结果,php,arrays,associative-array,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 rt

要为以下查询结果创建关联数组:

$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。我做错了什么?我怎样才能做到这一点?
提前感谢

因为您的函数
array\u push\u assoc()

试试这个:

   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个关联数组的数组

您的代码应更改为:

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']));           

}

... 因为
$dataArr
由4个子数组组成,它们是
store\u id
store\u name
store\u logo
deals\u image
?只需执行
var\u dump($dataArr)
并亲自查看即可。@MatteoTassinari但表中有28行,每28行应该产生一个结果。我想为每一行设置键值-store\u id、store\u name、store\u logo、deals\u image。那么,解决方案是什么,以便我可以为每28行设置store\u id、store\u name、store\u logo和deals\u image键?我尝试了你的代码。是否缺少什么?因为我收到一个错误-
请求的资源上不存在“Access Control Allow Origin”头。
我获取了您的最新代码,但它再次抛出相同的错误