douge3492 2015-02-28 15:22
浏览 88
已采纳

正确使用in_array与数组内部的数组

I want to use in_array to check multiple integers, e.g checking integer "427971780" key: amenityId if exist anywhere in the array of a array.

The structure of the array is:

array(43) {
[0]=>
 array(2) {
 ["amenityId"]=>
 int(427971780)
 ["amenity"]=>
 string(28) "24-timers fitnessfaciliteter"
}
[1]=>
 array(2) {
 ["amenityId"]=>
 int(359778141)
 ["amenity"]=>
 string(27) "Antal bygninger/højhuse -3"
}

I'm using this php trying to look into the arrays an finding the integer but it doesen't seem to work.

If I do a

var_dump($hotelAmenities[0]['amenityId']); 

it works fine. But I need it to be dynamic, looking up multiple integers. That's the reason for the IF sentence.

 for($x=0;$x<50;$x++){
if(in_array("427971780", $hotelAmenities[$x]['amenityId'])){
 echo "<li><i class='im im-bar'></i><span class='booking-item-feature-title'>Bar/Lounge</span></li>";
 }
 }

Can anyone spot for is wrong with the for/if code above?

  • 写回答

1条回答 默认 最新

  • dpnfxk251524 2015-02-28 15:49
    关注

    Your code is wrong. Try this.

    for ($x = 0; $x < 50; $x ++) {
        if (in_array('427971780', $hotelAmenities[$x])) {
            echo "<li><i class='im im-bar'></i><span class='booking-item-feature-title'>Bar/Lounge</span></li>";
        }
    }
    

    And try to use this method in_array() and multidimensional array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输