两个foreach 怎么循环?
我用的某多用户商城系统,现在想把店铺一级大类下面的二级类循环出来,同时二级大类下面自动读取当前二级大类中的推荐店铺,请问该怎么写。如下图:
代码如下:
<div class="louceng"><br /> <?php if(!empty($output['class_list']) && is_array($output['class_list'])){<br /> $i = 0;<br /> ?><br /><br /> <br /> <?php foreach($output['class_list'] as $k=>$v){<br /> <br /> $i++;<br /> ?><br /> <div class="floor_area"> <br /> <?php if ($_GET['cate_id'] == $v['sc_parent_id']){<br /> ?><br /> <h2 id="php-nbsp-echo-nbsp-i-F-nbsp-nbsp-php-nbsp-echo-nbsp-v-sc-name"><?php echo $i;?>F <?php echo $v['sc_name'];?></h2><br /> <?php if(!empty($output['store_list']) && is_array($output['store_list'])){?><br /> <ul><br /> <?php foreach($output['store_list'] as $skey => $store){?><br /> <br /> <li><a href="<?php echo ncUrl(array('act'=>'show_store','id'=>$store['store_id']),'store',$store['store_domain']);?>" title="" target="_blank"><span class="thumb size72"><br /> <img src="/static/imghwm/default1.png" data-src="<?php echo SiteUrl.'/'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);? alt="两个foreach 如何循环" >" class="lazy" /'.(ATTACH_PATH.DS.'ad'.DS.$store['shopad']);?>" onload="javascript:DrawImage(this,240,240);" /></span></a><br /> </li><br /> <br /> <?php }?><br /> </ul><br /> <div class="clear"></div><br /> <?php }?><br /> <?php }elseif (!isset($v['child']) && $output['class_list'][$_GET['cate_id']]['sc_parent_id'] == $v['sc_parent_id']){?><br /> <h2><?php echo $v['sc_name'];?></h2><br /> <?php }?><br /> </div><br /> <?php }?><br /> <br /> <?php }?><br /></div>
另外为什么不是从1开始?
------解决方案--------------------
你的代码不能实现吗?贴出 $output 的内容
$i++; 在 foreach($output['class_list'] as $k=>$v){ 循环中无条件执行
echo $i; 在 if ($_GET['cate_id'] == $v['sc_parent_id']){ 分支中有条件执行
因为条件可能不成立,所以 echo $i;就可能不连续
既然 $i 表示一个顺号,那么就应该删除 9 行的 $i++;
而 14 行的 echo $i; 改为 echo ++$i;

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
