我有一个如下所示的数组:
array(500) {
[1]=>
array(4) {
["name"]=>
string(17) "Final Fantasy VII"
["price"]=>
string(5) "11.69"
["shop"]=>
string(4) "9507"
["url"]=>
string(77) "https://de.gamespl...."
}
[2]=> ...
我想根据价格、商店、网址和名称检查指定的游戏是否仍保留在该数组中。我现在的问题是,如何搜索数组?
因为我没有游戏的 ID,所以要困难得多,因为数组像 [1] => array (4) ... [2] => ...
。
是否有更好的方法来堆叠数组? - 没有ID存储产品有什么优势吗? - 它会加速搜索吗?
因为我的重点是速度/优化,并且我需要最快的方式来完成此搜索...因为我必须检查 5000 个游戏,所以我需要最快的方式。
有谁有想法或可以告诉我哪一种是最快的方法以及如何在此数组中搜索?
最佳答案
您可以这样做,首先获取所需的所有列,然后在数据中作为完整数组进行搜索。
<?php
ini_set("display_errors", 1);
$array=array(
array(
"name"=> "x",
"price"=> "12000",
"shop"=> "212121",
"url"=> "http://www.someurl.com",
),
array(
"name"=> "xy",
"price"=> "11000",
"shop"=> "1212121",
"url"=> "http://www.someotherurl.com",
)
);
// date need to search for
$gameName="xy";
$gamePrice="11000";
$gameURL="http://www.someotherurl.com";
$gameId="1212121";
//Creating array from data for searching.
$dataToSearch=array(
"name"=> "xy",
"price"=> "11000",
"url"=> "http://www.someotherurl.com",
"shop"=> "1212121",
);
if(in_array($dataToSearch, $array))
{
echo "Game found!";
}
关于php - 如何在二维数组中进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43340904/