PHP比较foreach循环中Array的值

时间:2017-01-29 20:51:26

标签: php arrays foreach

我有一个这样的数组(修剪过)分配给$rows

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次迭代中的数组值。

1 个答案:

答案 0 :(得分:0)

您正在使用一些不必要的迭代,为密钥为房间名称的房间创建帮助器字典:

#ifndef LinkedList_h
#define LinkedList_h

#include <iostream>

using namespace std;

struct node {
    node * prev;
    int data;
    node * next;

};

class LinkedList {

private:
    //pointers to point to front and end of Linked List
    static node * front; //the error is coming from here
    static node * rear;  //the error is coming from here
public:
    static void insert_front(int data);
};
#endif

稍后您可以使用此dict查找信息,例如:

#include "LinkedList.h"

//insert int to front
void LinkedList::insert_front(int data) {

    node *q = nullptr;
    //If the list is empty
    if (front == nullptr && rear == nullptr) {
        q = new node;
        q->prev = nullptr;
        q->data = data;
        q->next = nullptr;
        front = q;
        rear = q;
        q = nullptr;
    }
    //If there is only one node in list
    //...
    //If there are at least 2 nodes in list
    //...

}