我有一個這樣的陣列(修剪)分配給$rows
:PHP從數組中foreach循環比較值
array (size=9)
0 =>
array (size=2)
'room_category' => string 'MALE GENERAL WARD' (length=17)
'vacant_beds' => string 'MG-8,MG-2,MG-4,MG-6,MG-7' (length=24)
1 =>
array (size=2)
'room_category' => string 'FEMALE GENERAL WARD' (length=19)
'vacant_beds' => string 'FG-4,FG-1,FG-2,FG-3' (length=19)
2 =>
array (size=2)
'room_category' => string 'MOTHER CHILD WARD' (length=17)
'vacant_beds' => string 'MC-2,MC-4,MC-5,MC-6' (length=19)
3 =>
array (size=2)
'room_category' => string 'TWIN' (length=4)
'vacant_beds' => string 'TW-A1,TW-A2,TW-B2,TW-C1,TW-C2' (length=29)
4 =>
array (size=2)
'room_category' => string 'NICU' (length=4)
'vacant_beds' => string 'NICU-6,NICU-1,NICU-7,NICU-3,NICU-8,NICU-4,NICU-5' (length=48)
5 =>
array (size=2)
'room_category' => string 'CLASSIC' (length=7)
'vacant_beds' => string 'CL-6,CL-8,CL-4,CL-5' (length=19)
6 =>
array (size=2)
'room_category' => string 'DELUXE' (length=6)
'vacant_beds' => string 'DLX-5,DLX-6' (length=11)
7 =>
array (size=2)
'room_category' => string 'EXECUTIVE' (length=9)
'vacant_beds' => null
8 =>
array (size=2)
'room_category' => string 'AC GENERAL WARD' (length=15)
'vacant_beds' => string 'AG-5,AG-1,AG-2,AG-3,AG-4' (length=24)
這個陣列被保存在$occupancies
array (size=21)
0 =>
array (size=7)
'room_name' => string 'CL-12' (length=5)
'name' => string 'SHIBNATH PATEL' (length=14)
'ipd' => string '0724/15' (length=7)
'relation' => string '' (length=0)
'mobile' => string '9178174433' (length=10)
'district_city' => string 'SUNDARGARH' (length=10)
'admission_date' => string '2015-06-15 11:30:00' (length=19)
1 =>
array (size=7)
'room_name' => string 'AG-7' (length=4)
'name' => string ' SHILPA GUPTA ' (length=14)
'ipd' => string '0734/15' (length=7)
'relation' => string 'D/0- PRADEEP KUMAR GUPTA ' (length=25)
'mobile' => string '9937384641' (length=10)
'district_city' => string 'SAMBALPUR' (length=9)
'admission_date' => string '2015-06-15 13:45:00' (length=19)
現在我想顯示該數組中的值取決於我在foreach循環中的room_name的匹配。
我的代碼是這樣的:
$max_count = 0;
$new_rows =[];
foreach ($rows as $key=>$row){
$elements=[];
if(!empty($row['vacant_beds'])){
$elements = explode(',',$row['vacant_beds']);
if(sizeof($elements) > $max_count){
$max_count = sizeof($elements);
}
}
$new_rows[]= [
'name'=>$row['room_category'],
'elements'=>$elements,
];
}
foreach ($new_rows as $row){
echo '<tr>';
echo '<td>'.$row['name'] . '</td>';
foreach($row['elements'] as $e){
if ($occupancy['room_name']==$e) {
echo "<td style='background-color:#CCFFFF;'>
<div class='occupied'>MG-1<div id='occupied-hover'>
Patient Name - $occupancy_patient_name <br> IPD No. - $occupancy_ipd
<br>
Guardian - $occupancy_relation <br> Mobile - $occupancy_mobile <br>
Admission Date - $occupancy_admission_date
</div></div> </td>";
}else{
echo "<td style='background-color:#FFFFCC;'/>$e </td>";
}
我的問題是如何我可以遍歷$rows
數組中的值正確地在上述foreach
迭代顯示數組值。
從您的示例'vacant_beds'和'room_category'沒有在數組中定義。 –
你是對的,我跳過了,現在更新它。 – Pawan