最近在實現最土團購系統的價格排序功能,需要對$oc數組進行擴展,經過測試用下面的方法即可。
核心程式碼如下:
<?<span>php </span><span>$now</span>=<span>time</span><span>(); </span><span>$oc</span> = <span>array</span><span>( </span>'team_type' => 'normal', "begin_time < '{<span>$now</span>}'", "end_time > '{<span>$now</span>}'",<span> ); </span><span>$p</span>="p2"<span>; </span><span>$pp1</span>=""<span>; </span><span>$pp2</span>=""<span>; </span><span>$now</span> = <span>time</span><span>(); </span><span>switch</span> (<span>$p</span><span>) { </span><span>case</span> "p1": <span>$pp1</span>="team_price < 10"<span>; </span><span>$pp2</span>=""<span>; </span><span>break</span><span>; </span><span>case</span> "p2": <span>$pp1</span>="team_price > 10"<span>; </span><span>$pp2</span>="team_price < 50"<span>; </span><span>break</span><span>; </span><span>case</span> "p3": <span>$pp1</span>="team_price > 50"<span>; </span><span>$pp2</span>="team_price < 100"<span>; </span><span>break</span><span>; </span><span>case</span> "p4": <span>$pp1</span>="team_price > 100"<span>; </span><span>$pp2</span>="team_price < 200"<span>; </span><span>break</span><span>; </span><span>case</span> "p5": <span>$pp1</span>="team_price > 200"<span>; </span><span>$pp2</span>=""<span>; </span><span>break</span><span>; </span><span>default</span>: <span>$pp1</span>=""<span>; </span><span>$pp2</span>=""<span>; } </span><span>if</span>(!<span>empty</span>(<span>$pp1</span><span>)){ </span><span>array_push</span>(<span>$oc</span>,<span>$pp1</span><span>); } </span><span>if</span>(!<span>empty</span>(<span>$pp2</span><span>)){ </span><span>array_push</span>(<span>$oc</span>,<span>$pp2</span><span>); } </span><span>print_r</span>(<span>$oc</span><span>); </span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span>?>
因為是多條件查詢所以需要先判斷是否為空,然後再加入陣列裡面。
推薦:http://www.cnblogs.com/roucheng/p/3528396.html
以上就介紹了 php 陣列動態添加實現程式碼最土團購系統的價格排序,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。