2017-10-21 30 views
-1

我有多個數組, 使用反序列化函數從數據庫檢索數據。 據我所知,使用foreach(array_combine)我可以得到想要的結果,但array_combine只能有2個參數。 所以我需要一個解決方案,我可以檢索多個數組值,將其組合並顯示在我的表中。這裏需要幫助來顯示使用array_combine或任何其他方法可用的多個反序列化值

例如僅2個參數工作正常,此代碼工作正常,因爲我只通過2個參數,以array_combine

$b_destinations = unserialize(base64_decode($rs['b_destinations'])); 
 
$b_nights = unserialize(base64_decode($rs['b_nights'])); 
 

 
foreach (array_combine($b_destinations[0], $b_nights[0]) as $desti=>$nights) 
 
{ 
 
echo" 
 
<tr><td>Detination</td><td> ".$desti."</td><td>Nights : ".$nights."</td></tr>"; 
 
}

,但我想在這裏顯示多個數組值

$b_hdest = unserialize(base64_decode($rs['b_hdest'])); 
 
$b_hname = unserialize(base64_decode($rs['b_hname'])); 
 
$b_hadd = unserialize(base64_decode($rs['b_hadd'])); 
 
$b_hphone = unserialize(base64_decode($rs['b_hphone'])); 
 
$b_hin = unserialize(base64_decode($rs['b_hin'])); 
 
$b_hout = unserialize(base64_decode($rs['b_hout'])); 
 
$b_hroom = unserialize(base64_decode($rs['b_hroom'])); 
 
$b_hmeal = unserialize(base64_decode($rs['b_hmeal'])); 
 
$b_haextra = unserialize(base64_decode($rs['b_haextra'])); 
 
$b_hcextra = unserialize(base64_decode($rs['b_hcextra'])); 
 
$b_hspecial = unserialize(base64_decode($rs['b_hspecial'])); 
 
$b_hincl = unserialize(base64_decode($rs['b_hincl'])); 
 

 
$hcnt=1; 
 

 
//which method or function use here to display this desired result 
 
foreach or any other method loop 
 
{ 
 
echo "<tr><td colspan='4'>Hotel Details ".$hcnt."</td></tr> 
 
<tr><td>Destination : ".$b_hdest."</td><td>Hotel Name : ".$b_hname."</td><td>Add : ".$b_hadd."</td><td>Phone : ".$b_hphone."</td></tr> 
 

 
<tr><td>Check In : ".$b_hin."</td><td>Check Out : ".$b_hout."</td><td>Rooms : ".$b_hroom."</td><td>Meal : ".$b_hmeal."</td></tr> 
 

 
<tr><td>Extra Adult : ".$b_haextra."</td><td>Extra Child : ".$b_hcextra."</td><td>Special : ".$b_hspecial."</td><td>Inclusion : ".$b_hincl."</td></tr>"; 
 
$hcnt++; 
 
}

和我需要的結果是這樣的。 我已經使用array_combine,但它只適用於2 paramneters 是否有任何其他方法或方法來做到這一點。

.mytable table, td, th {  
 
    border: 1px solid #ddd; 
 
    text-align: left; 
 
} 
 

 
.mytable table { 
 
    border-collapse: collapse; 
 
    width: 100%; 
 
} 
 

 
.mytable th, td { 
 
    padding: 15px; 
 
}
<table class='mytable'> 
 
<tr><td colspan='4'>Hotel Details 1</td></tr> 
 
<tr><td>Destination : Kerala</td><td>Hotel Name : Beach Resort</td><td>Add : hotel address</td><td>Phone : 7447344789</td></tr> 
 

 
<tr><td>Check In : 10/11/2017</td><td>Check Out : 15/11/2017</td><td>Rooms : 4</td><td>Meal : MAP</td></tr> 
 

 
<tr><td>Extra Adult : 1</td><td>Extra Child : 1</td><td>Special : no special</td><td>Inclusion : no inclusion</td></tr> 
 
<tr><td colspan='4'>Hotel Details 2</td></tr> 
 
<tr><td>Destination : Kerala</td><td>Hotel Name : Beach Resort</td><td>Add : hotel address</td><td>Phone : 7447344789</td></tr> 
 

 
<tr><td>Check In : 10/11/2017</td><td>Check Out : 15/11/2017</td><td>Rooms : 4</td><td>Meal : MAP</td></tr> 
 

 
<tr><td>Extra Adult : 1</td><td>Extra Child : 1</td><td>Special : no special</td><td>Inclusion : no inclusion</td></tr> 
 
<tr><td colspan='4'>Hotel Details 3</td></tr> 
 
<tr><td>Destination : Kerala</td><td>Hotel Name : Beach Resort</td><td>Add : hotel address</td><td>Phone : 7447344789</td></tr> 
 

 
<tr><td>Check In : 10/11/2017</td><td>Check Out : 15/11/2017</td><td>Rooms : 4</td><td>Meal : MAP</td></tr> 
 

 
<tr><td>Extra Adult : 1</td><td>Extra Child : 1</td><td>Special : no special</td><td>Inclusion : no inclusion</td></tr> 
 
</table>

+0

只要使用不同的密鑰,您可以用'arrayOfArray =數組1 +數組2 + array3 + array4 + array5'。 [例如。](https://stackoverflow.com/questions/2140090/operator-for-array-in-php) –

+0

爲什麼base64編碼?我建議你如何存儲這個數據是非常優化的,首先修復 – rtfm

+0

,因爲我接受來自textarea的數據進行存儲,所以當我使用「亞洲最大」等名稱時,因爲我在存儲數據時在數據庫中使用序列化函數,它給了我錯誤bcoz的,所以我用base64 enocde方法來存儲它properyl和解碼,以適當地檢索數據 – Revati

回答

0

嘗試以下方法,讓我知道

for($i=0;$i<count($b_hdest[0]);$i++){ 
 
echo" 
 
    <tr><td>Hotel Details ".($i+1)."</td></tr> 
 
    <tr><td>Location : ".$b_hdest[0][$i]."</td><td>Name : ".$b_hname[0][$i]."</td><td>Address : ".$b_hadd[0][$i]."</td><tr> 
 
    <tr><td>Phone : ".$b_hphone[0][$i]."</td><td>CheckIn : ".$b_hin[0][$i]."</td><td>CheckOut : ".$b_hout[0][$i]."</td><tr> 
 
    <tr><td>Rooms : ".$b_hroom[0][$i]."</td><td>Meals : ".$b_hmeal[0][$i]."</td><td>Extra Adult : ".$b_haextra[0][$i]."</td><tr> 
 
    <tr><td>Extra Child : ".$b_hcextra[0][$i]."</td><td>Special : ".$b_hspecial[0][$i]."</td><td>Inclusion : ".$b_hincl[0][$i]."</td><tr> 
 
"; 
 
}

+0

先生我已經試過這個,它給了我數組字符串轉換錯誤 – Revati

+0

先生我已經試過這個,它給我數組字符串轉換錯誤 – Revati

+0

感謝您的幫助先生,它被解決了工作喜歡的魅力。已經由@ abhishek-aggrawal先生解決了。 – Revati

相關問題