I have two separate arrays like the following:
$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"),
);
I want to compare and match the name and description of the two arrays based on the id number. I came up with the following code:
foreach($array as $value){
foreach($array1 as $value1){
if($value['id'] == $value1['id']){
echo "name is ".$value['name']. " and description is ".$value1['description']."<p>";
}
}
}
That displays the outcome:
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
This is exactly what I wanted but I was wondering if there is a way to decrease the runtime of the code since I used foreach twice, it will need to go through each array to check if the id number match. I will have an array that will have over 100 arrays in it and I will need to compare two arrays to find matching values. Therefore it might be slow. Is there a quicker way other than foreach to compare two arrays?