如何检查子数组名称是否存在于php中?

时间:2018-11-15 08:40:26

标签: php php-5.6 php-5.5

我有$ data之类的JSON格式数据

       {
          Name: ....
          ...
          Rooms :
           [
            {
               ...
            },
             {
               ...
            }
           ]
       }

我想做喜欢的

    $RoomArray =  $data[0]['Rooms'];
    $RoomCount = 0;
    if (['Rooms'] exist)
    $RoomCount = count($RoomCount));

在计算房间数之前,我想首先检查名称为Rooms的子数组是否存在。如果存在名称为Rooms的子数组,则它应该计数为$RoomCount = count($RoomCount));,否则$RoomCount应该为零。

请帮助!!!

这是实际数据

            array(1) { [0]=> object(MongoDB\Model\BSONDocument)#112 (1) { ["storage":"ArrayObject":private]=> array(19) { ["_id"]=> object(MongoDB\BSON\ObjectId)#12 (1) { ["oid"]=> string(24) "5be2d9aad2ccda0fdc006a63" } ["Name"]=> string(19) "Automatic Dormitory" ["BuildingId"]=> string(8) "abc12345" ["BuildingNumber"]=> string(6) "123456" ["Image"]=> string(0) "" ["Address"]=> string(8) "abcdefgh" ["NoOfFloors"]=> int(3) ["NamingConvention"]=> string(9) "Alphabets" ["NumberOfBays"]=> string(1) "2" ["RoomsPerBay"]=> string(1) "2" ["RoomCreation"]=> string(9) "Automatic" ["Approved"]=> string(3) "Yes" ["Bay"]=> object(MongoDB\Model\BSONArray)#18 (1) { ["storage":"ArrayObject":private]=> array(2) { [0]=> object(MongoDB\Model\BSONDocument)#15 (1) { ["storage":"ArrayObject":private]=> array(6) { ["BayName"]=> string(3) "fdg" ["FloorNumbers"]=> string(1) "1" ["NumberOfRooms"]=> string(1) "2" ["StartNumber"]=> string(3) "111" ["BayId"]=> object(MongoDB\BSON\ObjectId)#11 (1) { ["oid"]=> string(24) "5be2d9aad2ccda0fdc006a61" } ["IsDeleted"]=> string(2) "No" } } [1]=> object(MongoDB\Model\BSONDocument)#17 (1) { ["storage":"ArrayObject":private]=> array(6) { ["BayName"]=> string(3) "ffh" ["FloorNumbers"]=> string(1) "2" ["NumberOfRooms"]=> string(1) "2" ["StartNumber"]=> string(3) "123" ["BayId"]=> object(MongoDB\BSON\ObjectId)#16 (1) { ["oid"]=> string(24) "5be2d9aad2ccda0fdc006a62" } ["IsDeleted"]=> string(2) "No" } } } } ["SchoolId"]=> object(MongoDB\BSON\ObjectId)#19 (1) { ["oid"]=> string(24) "5ad056a5bb2c5610f8005794" } ["Rooms"]=> object(MongoDB\Model\BSONArray)#35 (1) { ["storage":"ArrayObject":private]=> array(6) { [0]=> object(MongoDB\Model\BSONDocument)#21 (1) { ["storage":"ArrayObject":private]=> array(7) { ["RoomId"]=> string(24) "5be2d9aad2ccda0fdc006a64" ["FloorNumber"]=> string(1) "1" ["RoomNumber"]=> string(3) "111" ["RoomType"]=> string(0) "" ["NumberOfBeds"]=> string(1) "0" ["Available"]=> int(0) ["BayId"]=> object(MongoDB\BSON\ObjectId)#20 (1) { ["oid"]=> string(24) "5be2d9aad2ccda0fdc006a61" } } } [1]=> object(MongoDB\Model\BSONDocument)#24 (1) { ["storage":"ArrayObject":private]=> array(7) { ["RoomId"]=> string(24) "5be2d9aad2ccda0fdc006a65" ["FloorNumber"]=> string(1) "1" ["RoomNumber"]=> string(3) "112" ["RoomType"]=> object(MongoDB\BSON\ObjectId)#22 (1) { ["oid"]=> string(24) "5b0ceee0bb2c561448001cc5" } ["NumberOfBeds"]=> string(1) "1" ["Available"]=> int(0) ["BayId"]=> object(MongoDB\BSON\ObjectId)#23 (1) { ["oid"]=> string(24) "5be2d9aad2ccda0fdc006a61" } } } [2]=> object(MongoDB\Model\BSONDocument)#26 (1) { ["storage":"ArrayObject":private]=> array(7) { ["RoomId"]=> string(24) "5be2d9aad2ccda0fdc006a66" ["FloorNumber"]=> string(1) "2" ["RoomNumber"]=> string(3) "123" ["RoomType"]=> string(0) "" ["NumberOfBeds"]=> string(1) "0" ["Available"]=> string(1) "0" ["BayId"]=> object(MongoDB\BSON\ObjectId)#25 (1) { ["oid"]=> string(24) "5be2d9aad2ccda0fdc006a62" } } } [3]=> object(MongoDB\Model\BSONDocument)#28 (1) { ["storage":"ArrayObject".............

0 个答案:

没有答案