doutiao2540 2018-08-19 12:05
浏览 8

如何用php搜索数组

im trying to make a search function for my website, but i cannot get it to work. here is my code:

$search_string = "html documentation";
$search_string = explode(' ', $search_string);

$array = "


  Array (
    [0] => Array (
      [0] => 404
      [1] => notfound
      [2] => error
    )

    [1] => Array (
      [0] => 403
      [1] => forbidden
      [2] => error
    )

    [2] => Array (
      [0] => home
    )

    [3] => Array (
      [0] => hcjp
      [1] => html
      [2] => css
      [3] => js
      [4] => php
      [5] => learning
      [6] => documentation
    )

    [4] => Array (
      [0] => about
    )

    [5] => Array (
      [0] => search
      [1] => search on no conditions
    )

    [6] => Array (
      [0] => search
      [1] => search on label
    )
  )

";

$search_result = in_array($search_string, $array);
print_r($search_result);

i am not getting any output, my goal is to get some sort of path like: [3], so that i know that:

$search_result[3]

contains info the user requested.

thanks

  • 写回答

3条回答 默认 最新

  • douyan4243 2018-08-19 12:31
    关注

    One way to do it might be to loop the arrays in $array and use array_intersect. If there is a match then return the $key from $array

    foreach ($array as $key => $a) {
        if (count(array_intersect($a, $search_string)) > 0) {
            echo $key;
        }
    }
    

    Demo

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度