简体   繁体   中英

php compare two associative array value

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 ie DA, PA, MozRank, Linksln, and so on...

I hope some genius can help me

Thank you

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');

Check this custom PHP script returns you matched url from two array

 <?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 
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM