Reputation: 461
MySQL array
CSV file array
I want to match this two different associative arrays "Url" value. Also I want remaining array value of CSV file i.e. DA, PA, MozRank, Linksln, and so on...
I hope some genius can help me
Thank you
Upvotes: 1
Views: 2554
Reputation: 4547
<?php
/**
* Function for match url and return data
*/
function matchArray($array1, $array2)
{
$index = 0;
$return = array();
$count1 = count($array1);
$count2 = count($array2);
for($i=0; $i < $count1; $i++)
{
for($j=0; $j < $count2; $j++)
{
if($array1[$i]['url'] == $array2[$j]['url']) {
$return[$index]['url'] = $array2[$j]['url']; // Add index which you want to get
$return[$index]['DA'] = $array2[$j]['DA']; // Add index which you want to get
$return[$index]['PA'] = $array2[$j]['PA'];
$return[$index]['MozRank'] = $array2[$j]['MozRank'];
}
$index ++;
}
}
echo "<pre>";
print_r($return); // this will return matched url form two array
}
$array1 = array(array('url' => 'AllConsuming.net' ),array('url' => 'app.brand-mention.com'),array('url' => 'www.microsoft.com'));
$array2 = array(array('url' => 'AllConsuming.net', 'DA' => 48, 'PA'=> 54.4, 'MozRank'=> 5.4),array('url' => 'www.microsoft.com', 'DA' => 31.7, 'PA'=> 54.4, 'MozRank'=> 5.4));
matchArray($array1, $array2); // calling function
?>
Upvotes: 2
Reputation: 175
If you want to know which entries are indentical, proceed this way :
$array1 = array_column($ar1, 'url'); // return array('AllConsuming.net', 'http://app.brand-mention.com', 'https://www.microsoft.com', 'otherstuff')
$array2 = array_column($ar2, 'url'); // return array('AllConsuming.net', 'TravelIntelligence.net', 'otherstuff')
// compare these 2 arrays
$comp = array_intersect($aray1, $array2);
var_dump($comp); // output array('AllConsuming.net', 'otherstuff');
Upvotes: 2