1、在test.php文件內(nèi),使用header設(shè)置test.php執(zhí)行的編碼為utf8,避免輸出中文的時(shí)候出現(xiàn)亂碼。
成都創(chuàng)新互聯(lián)公司是專業(yè)的江陽(yáng)網(wǎng)站建設(shè)公司,江陽(yáng)接單;提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行江陽(yáng)網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
2、在test.php文件內(nèi),創(chuàng)建一個(gè)測(cè)試的數(shù)組,例如,定義一個(gè)分類的數(shù)組,其對(duì)應(yīng)的索引值分別為0,4,8。
3、在test.php文件內(nèi),使用array_values()方法將上一步的數(shù)據(jù)重新排序,并且從0開(kāi)始,把重新排序的數(shù)組保存在$result變量中。
4、在test.php文件內(nèi),使用foreach方法遍歷數(shù)組,其中$k為索引值,$v為索引值對(duì)應(yīng)的數(shù)組值。
5、在test.php文件內(nèi),使用echo方法輸出數(shù)組中的索引值和對(duì)應(yīng)的數(shù)組值即可。
既然是遍歷,那就將數(shù)據(jù)庫(kù)指針先移到第一條記錄,逐次取出數(shù)據(jù)進(jìn)行運(yùn)算,下移指針,直到庫(kù)結(jié)束。
通常的代碼如下:
mysql_data_seek($result,0);//指針復(fù)位
while($row=mysql_fetch_array($result))?{?
//對(duì)每行記錄進(jìn)行運(yùn)算?處理,如?:echo?$row['name']."br?/";?
}
有2個(gè)辦法,第一種直接使用sql的多表聯(lián)查,效率高,但是得到的數(shù)據(jù)table1會(huì)被擴(kuò)展成table2一樣的條目數(shù) 要再次處理
select?*?from?table1?a,table2?b?where?a.orderid?=?b.orderid
第二種方法,先得到table11的數(shù)據(jù),在循環(huán)中匹配table2到一個(gè)新的列名中
$conn?=?mysqli_connect("127.0.0.1",?"root",?"123",?"test");
$sql?=?"select?*?from?table1";
$rs?=?mysqli_query($conn,?$sql);
$Arr?=?array();
while?($row?=?mysqli_fetch_assoc($rs))?{
$sql?=?"select?*?from?table2?where?orderid?="?.$row["orderid"];
$row["order_sku"]?=?mysqli_fetch_all(mysqli_query($conn,?$sql),?MYSQLI_ASSOC);
$Arr[]?=?$row;
}
print_r($Arr)
如果你是剛開(kāi)始學(xué)php 建議直接拋棄mysql用mysqli 因?yàn)镻HP5.5已經(jīng)廢棄mysql方法了
第一、foreach()
foreach()是一個(gè)用來(lái)遍歷數(shù)組中數(shù)據(jù)的最簡(jiǎn)單有效的方法。
?php
$urls= array('aaa','bbb','ccc','ddd');
foreach ($urls as $url){
echo "This Site url is $url! br /";
}
?
顯示結(jié)果:
This Site url is aaa
This Site url is bbb
This Site url is ccc
This Site url is ddd
第二、while() 和 list(),each()配合使用。
?php
$urls= array('aaa','bbb','ccc','ddd');
while(list($key,$val)= each($urls)) {
echo "This Site url is $val.br /";
}
?
顯示結(jié)果:
?
This Site url is aaa
This Site url is bbb
This Site url is ccc
This Site url is ddd
第三、for()運(yùn)用for遍歷數(shù)組
?php
$urls= array('aaa','bbb','ccc','ddd');
for ($i= 0;$i count($urls); $i++){
$str= $urls[$i];
echo "This Site url is $str.br /";
}
?
顯示結(jié)果:
This Site url is aaa
This Site url is bbb
This Site url is ccc
This Site url is ddd
這幾種遍歷數(shù)組的方法哪個(gè)更快捷些呢,下面做個(gè)簡(jiǎn)單的測(cè)試就明白了
=========== 下面來(lái)測(cè)試三種遍歷數(shù)組的速度 ===========
一般情況下,遍歷一個(gè)數(shù)組有三種方法,for、while、foreach。其中最簡(jiǎn)單方便的是foreach。下面先讓我們來(lái)測(cè)試一下共同遍歷一個(gè)有50000個(gè)下標(biāo)的一維數(shù)組所耗的時(shí)間。
?php
$arr= array();
for($i= 0; $i 50000; $i++){
$arr[]= $i*rand(1000,9999);
}
function GetRunTime()
{
list($usec,$sec)=explode(" ",microtime());
return ((float)$usec+(float)$sec);
}
######################################
$time_start= GetRunTime();
for($i= 0; $i count($arr); $i++){
$str= $arr[$i];
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of for:'.round($time_used, 7).'(s)br /br /';
unset($str, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
while(list($key, $val)= each($arr)){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of while:'.round($time_used, 7).'(s)br /br /';
unset($str, $key, $val, $time_start, $time_end, $time_used);
######################################
$time_start= GetRunTime();
foreach($arr as$key= $val){
$str= $val;
}
$time_end= GetRunTime();
$time_used= $time_end- $time_start;
echo 'Used time of foreach:'.round($time_used, 7).'(s)br /br /';
?
測(cè)試結(jié)果:
Used time of for:0.0228429(s)
Used time of while:0.0544658(s)
Used time of foreach:0.0085628(s)
結(jié)果表明,對(duì)于遍歷同樣一個(gè)數(shù)組,foreach速度最快,最慢的則是while。從原理上來(lái)看,foreach是對(duì)數(shù)組副本進(jìn)行操作(通過(guò)拷貝數(shù)組),而while則通過(guò)移動(dòng)數(shù)組內(nèi)部指標(biāo)進(jìn)行操作,一般邏輯下認(rèn)為,while應(yīng)該比f(wàn)oreach快(因?yàn)閒oreach在開(kāi)始執(zhí)行的時(shí)候首先把數(shù)組復(fù)制進(jìn)去,而while直接移動(dòng)內(nèi)部指標(biāo)。),但結(jié)果剛剛相反。原因應(yīng)該是,foreach是PHP內(nèi)部實(shí)現(xiàn),而while是通用的循環(huán)結(jié)構(gòu)。所以,在通常應(yīng)用中foreach簡(jiǎn)單,而且效率高。在PHP5下,foreach還可以遍歷類的屬性。
希望能夠喜歡。
分享題目:php如何遍歷數(shù)據(jù)庫(kù)表 php遍歷數(shù)據(jù)庫(kù)表中內(nèi)容
文章鏈接:http://chinadenli.net/article10/dogcpdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁(yè)設(shè)計(jì)公司、ChatGPT、自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)