php比较两个关联数组值

时间:2017-03-20 11:33:56

标签: php

MySQL阵列

image

CSV文件数组

image

我想匹配这两个不同的关联数组" Url"值。另外我想要保留CSV文件的数组值,即DA,PA,MozRank,Linksln等......

我希望有些天才可以帮助我

谢谢

2 个答案:

答案 0 :(得分:2)

如果您想知道哪些条目是缩进的,请按以下方式进行:

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

答案 1 :(得分:2)

检查此自定义PHP脚本会从两个数组

返回匹配的URL
 <?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 
 ?>