在数组查询结果中搜索php/mysql

HP.*_*HP. 0 php mysql arrays

我有mysql结果($date_options)如下

Array (
    [0] => stdClass Object (
        [id] => 4
        [start_date] => 2010-09-29
    )
    [1] => stdClass Object (
        [id] => 13
        [start_date] => 2010-10-06
    )
)
Run Code Online (Sandbox Code Playgroud)

如果存在某些值,我需要能够在"id"字段中搜索.我尝试过这个并没有用:

array_search($event_id, $date_options, true)
Run Code Online (Sandbox Code Playgroud)

$event_id是有id值.我只想查看id而不是start_date

怎么解决这个?

谢谢

cas*_*nca 6

由于您已经在使用MySQL,为什么不选择您感兴趣的ID?这比检索所有记录然后在PHP中搜索更有效.


如果你仍然想这样做,你可以简单地迭代数组并手动检查:

foreach ($date_options as $result) {
  if ($result->id == $event_id) {
     // do something
  }
}
Run Code Online (Sandbox Code Playgroud)