php - PHP在比較兩個數組時提高速度

  显示原文与译文双语对照的内容
93 1

我有兩個獨立的數組,如下所示:

$array = array(
 array("id" =>"1","name" =>"name1"),
 array("id" =>"2","name" =>"name2"),
 array("id" =>"3","name" =>"name3"),
 array("id" =>"4","name" =>"name4"),
 array("id" =>"5","name" =>"name5"),
 array("id" =>"6","name" =>"name6"),
 array("id" =>"7","name" =>"name7"),
 array("id" =>"8","name" =>"name8"),
 array("id" =>"9","name" =>"name9"),
 array("id" =>"10","name" =>"name10"),
 array("id" =>"11","name" =>"name11"),
 array("id" =>"12","name" =>"name12"),
);
$array1 = array(
 array("id" =>"1","description" =>"description1"),
 array("id" =>"2","description" =>"description2"),
 array("id" =>"3","description" =>"description3"),
 array("id" =>"4","description" =>"description4"),
 array("id" =>"5","description" =>"description5"),
 array("id" =>"6","description" =>"description6"),
 array("id" =>"7","description" =>"description7"),
 array("id" =>"8","description" =>"description8"),
 array("id" =>"9","description" =>"description9"),
 array("id" =>"10","description" =>"description10"),
 array("id" =>"11","description" =>"description11"),
 array("id" =>"12","description" =>"description12"),
);

我想比較和匹配兩個數組的名稱和描述,基於id號。我給出了以下代碼:

foreach($array as $value){
 foreach($array1 as $value1){
 if($value['id'] == $value1['id']){
 echo"name is".$value['name']." and description is".$value1['description']."<p>";
 }
 }
 }

顯示結果的:

name is name1 and description is description1
name is name2 and description is description2
name is name3 and description is description3
name is name4 and description is description4
name is name5 and description is description5
name is name6 and description is description6
name is name7 and description is description7
name is name8 and description is description8
name is name9 and description is description9
name is name10 and description is description10
name is name11 and description is description11
name is name12 and description is description12

我想知道是否有辦法減少代碼的運行時間,因為我使用了foreach兩次,它需要通過每個 array 來檢查是否有匹配。我將有一個 array,它將有超過 100個數組,我需要比較兩個數組來查找匹配的值。所以可能會很慢。除了foreach之外,比較兩個數組還有更快捷的方法?

时间:原作者:0个回答

89 3

將兩個數組組合到一個的最簡單方法是對 array_column 進行兩次調用,然後調用 array_replace_recursive:

$array = array_column($array, null, 'id');
$array1 = array_column($array1, null, 'id');
$merged = array_replace_recursive($array, $array1);

然後,你可以以簡單地遍歷行,並處理一次單個項的演示:

foreach ($merged as $row) {
 echo"name is {$row['name']} and description is {$row['description']}", PHP_EOL;
}

名稱為 name1,描述為 description1

請參見 https://eval.in/1058846

原作者:
113 2

而不是使用兩個數組的多個數組,只需一次,併合並它們。全部取決於你得到什麼數據和你想擁有什麼數據。

$array = array(
 array("id" =>"1","name" =>"name1"),
 array("id" =>"2","name" =>"name2"),
 array("id" =>"3","name" =>"name3"),
 array("id" =>"4","name" =>"name4"),
 array("id" =>"5","name" =>"name5"),
 array("id" =>"6","name" =>"name6"),
 array("id" =>"7","name" =>"name7"),
 array("id" =>"8","name" =>"name8"),
 array("id" =>"9","name" =>"name9"),
 array("id" =>"10","name" =>"name10"),
 array("id" =>"11","name" =>"name11"),
 array("id" =>"12","name" =>"name12"),
);
$array1 = array(
 array("id" =>"1","description" =>"description1"),
 array("id" =>"2","description" =>"description2"),
 array("id" =>"3","description" =>"description3"),
 array("id" =>"4","description" =>"description4"),
 array("id" =>"5","description" =>"description5"),
 array("id" =>"6","description" =>"description6"),
 array("id" =>"7","description" =>"description7"),
 array("id" =>"8","description" =>"description8"),
 array("id" =>"9","description" =>"description9"),
 array("id" =>"10","description" =>"description10"),
 array("id" =>"11","description" =>"description11"),
 array("id" =>"12","description" =>"description12"),
);
$mergedArray = [];
foreach ($array as $row) {
 $mergedArray[$row["id"]] = $row;
}
foreach ($array1 as $row) {
 $mergedArray[$row["id"]]["description"] = $row["description"];
}
var_dump($mergedArray);

輸出

array(12) {
 [1]=>
 array(3) {
 ["id"]=>
 string(1)"1"
 ["name"]=>
 string(5)"name1"
 ["description"]=>
 string(12)"description1"
 }
 [2]=>
 array(3) {
 ["id"]=>
 string(1)"2"
 ["name"]=>
 string(5)"name2"
 ["description"]=>
 string(12)"description2"
 }
 [3]=>
 array(3) {
 ["id"]=>
 string(1)"3"
 ["name"]=>
 string(5)"name3"
 ["description"]=>
 string(12)"description3"
 }
 [4]=>
 array(3) {
 ["id"]=>
 string(1)"4"
 ["name"]=>
 string(5)"name4"
 ["description"]=>
 string(12)"description4"
 }
 [5]=>
 array(3) {
 ["id"]=>
 string(1)"5"
 ["name"]=>
 string(5)"name5"
 ["description"]=>
 string(12)"description5"
 }
 [6]=>
 array(3) {
 ["id"]=>
 string(1)"6"
 ["name"]=>
 string(5)"name6"
 ["description"]=>
 string(12)"description6"
 }
 [7]=>
 array(3) {
 ["id"]=>
 string(1)"7"
 ["name"]=>
 string(5)"name7"
 ["description"]=>
 string(12)"description7"
 }
 [8]=>
 array(3) {
 ["id"]=>
 string(1)"8"
 ["name"]=>
 string(5)"name8"
 ["description"]=>
 string(12)"description8"
 }
 [9]=>
 array(3) {
 ["id"]=>
 string(1)"9"
 ["name"]=>
 string(5)"name9"
 ["description"]=>
 string(12)"description9"
 }
 [10]=>
 array(3) {
 ["id"]=>
 string(2)"10"
 ["name"]=>
 string(6)"name10"
 ["description"]=>
 string(13)"description10"
 }
 [11]=>
 array(3) {
 ["id"]=>
 string(2)"11"
 ["name"]=>
 string(6)"name11"
 ["description"]=>
 string(13)"description11"
 }
 [12]=>
 array(3) {
 ["id"]=>
 string(2)"12"
 ["name"]=>
 string(6)"name12"
 ["description"]=>
 string(13)"description12"
 }
}
原作者:
74 4

不是真的,除非

你知道,兩個數組以相同的順序包含相同的id 。然後,你可以使用一個 array 中的鍵來引用另一個。

foreach ($array as $key => $value) {
 echo"name is $value[name] and description is $value1[$key][description]<p>";
}

你可以使用標識作為鍵以某種方式對兩個數組進行索引。你可以通過這種方式來構建它們,這取決於你如何獲得它們。例如當從資料庫結果提取行結果為 array 時,使用該欄位作為鍵。

while($row = $result->fetch()) {
 $array[$row['id']] = $row;
}
//then you can use the same foreach as above even if the results
//aren't in the same order.

( 如果數組真的來自資料庫,那麼應該考慮使用聯接並使用一個查詢。)

除這裡之外,如果你在找到匹配的內部循環之後,將加速你當前正在使用的嵌套 foreach

原作者:
74 5

可以通過執行以下操作減少對第二個 array的查找

$array = array(
 array("id" =>"1","name" =>"name1"),
 array("id" =>"2","name" =>"name2"),
 array("id" =>"3","name" =>"name3"),
 array("id" =>"4","name" =>"name4"),
 array("id" =>"5","name" =>"name5"),
 array("id" =>"6","name" =>"name6"),
 array("id" =>"7","name" =>"name7"),
 array("id" =>"8","name" =>"name8"),
 array("id" =>"9","name" =>"name9"),
 array("id" =>"10","name" =>"name10"),
 array("id" =>"11","name" =>"name11"),
 array("id" =>"12","name" =>"name12"),
);
$array1 = array(
 array("id" =>"1","description" =>"description1"),
 array("id" =>"2","description" =>"description2"),
 array("id" =>"3","description" =>"description3"),
 array("id" =>"4","description" =>"description4"),
 array("id" =>"5","description" =>"description5"),
 array("id" =>"6","description" =>"description6"),
 array("id" =>"7","description" =>"description7"),
 array("id" =>"8","description" =>"description8"),
 array("id" =>"9","description" =>"description9"),
 array("id" =>"10","description" =>"description10"),
 array("id" =>"11","description" =>"description11"),
 array("id" =>"12","description" =>"description12"),
);
$ids = array_column($array1, 'id');
foreach($array as $value){
 $index = array_search($value['id'], $ids);
 if(false!== $index){
 $value1 = $array1[$index];
 echo"name is".$value['name']." and description is".$value1['description']."<p>";
 }
}

這裡我們使用 array_column 來獲取來自第二個 array的id的array 。因為 $ids array 是從其他 array $array1 派生的,所以兩個數字鍵之間的關聯。

然後我們可以使用第一個 array 中的當前條目輕鬆搜索 $ids,找到索引並使用它只提取我們想要的第二個 array 行。

這樣就消除了修改 array 結構的需要,並大大簡化了。但是,如果你在id中有重複的array,你將遇到問題。如果它們來自一個正確索引的資料庫,這可能不是一個問題。

主要的是,它消除了第二個foreach對大多數無用項的需求。如果你在這裡加入了和 else,你甚至可以錯誤檢查第二個 array,如果有一個標識符。

沙箱管理器

輸出

name is name1 and description is description1
name is name2 and description is description2
name is name3 and description is description3
name is name4 and description is description4
name is name5 and description is description5
name is name6 and description is description6
name is name7 and description is description7
name is name8 and description is description8
name is name9 and description is description9
name is name10 and description is description10
name is name11 and description is description11
name is name12 and description is description12

我應該提到這是非常有效的,因為它是無效的HTML ( 你將有 12個打開P 標籤不關閉 above )

echo"name is".$value['name']." and description is".$value1['description']."<p>";

如果你有重複的id,則為

最後如果可以以有副本,則可以以在列上使用 array_intersect,然後在這樣的交集上重複使用:

foreach($array as $value){
 $intersect = array_intersect($ids, [$value['id']]);
 foreach($intersect as $index=>$id){
 $value1 = $array1[$index];
 if(false!== $index){
 $value1 = $array1[$index];
 echo"name is".$value['name']." and description is".$value1['description']."<p>";
 }
 }
}

沙箱管理器

在原始代碼中的迭代次數仍然要少。

原作者:
...