PHP - 搜索一个具有值数组的关联数组,然后创建一个具有匹配结果的新数组


PHP - Search one associative array with an array of values, then create a new array with matching results

我有一个关联数组,如下所示:

{
  "organizations":[
    {
      "name":"10sheet",
      "owner_name":"Me"
    },
    {
      "name":"12Gigs",
      "owner_name":"Me"
    },
      "name":"24'/7 Card",
      "owner_name":"Me as well"
    },
}
我想使用第一个数组中的名称字段

与以下关联数组中的名称字段进行搜索:

[{"name": "Wetpaint",
  "permalink": "wetpaint",
  "category_code": "web"},
 {"name": "AdventNet",
  "permalink": "adventnet",
  "category_code": "enterprise"},
 {"name": "Zoho",
  "permalink": "zoho",
  "category_code": "software"},
 {"name": "Digg",
  "permalink": "digg",
  "category_code": "web"},
 {"name": "24/7 Card",
  "permalink": "247-card",
  "category_code": "other"}]

如果存在匹配项,则获取永久链接字段中的值并将其放入新数组中。此外,在第一个数组中存在转义问题,例如 24/7 而不是 24/7,搜索不应区分大小写。我正在尝试用PHP编写它,使用这两个API正在杀死我。

感谢您的帮助!

-格雷格

未经测试,但应该可以工作:

<?php
$result = array();
foreach($obj["organizations"] as $o) {
    foreach($obj2 as $o2) {
        if(stristr($o["name"], $o2["name"])) {
            $result[] = $o2["permalink"];
        }
    }
}
?>

$obj是你的第一个数组,$obj 2 第二个 .. $result 包含匹配项。