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 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下