使用以下代码作为示例:
$wikisearch = "http://en.wikipedia.org/w/api.php?action=opensearch&search=inception";
$wikisearchlist = file_get_contents($wikisearch);
echo $wikisearchlist;
我明白了:
[
"inception",
[
"Inception",
"Inception Motorsports",
"Inception of Darwin's theory",
"Inception (soundtrack)",
"Inception (McCoy Tyner album)",
"Inception (Download album)",
"Inception/Nostalgia",
"Inception date",
"Inception (disambiguation)"
]
]
我想首先在开始时删除查询(“inception”),然后解码JSON以创建所有结果的数组。然后,删除其中不包含'(音轨)'和'(下载专辑)'的数组元素(第一个结果除外),因此最终数组看起来像:
[0] => "Inception"
[1] => "Inception (soundtrack)"
[2] => "Inception (Download album)"
最好的方法是什么? 感谢
答案 0 :(得分:1)
$json = json_decode($wikisearchlist);
$results = $json[1];
$first = array_shift($results);
$filtered = array_filter($results, function($result) {
return strpos($result, '(soundtrack)') !== false
|| strpos($result, '(Download album)') !== false;
});
array_unshift($filtered, $first);