dqqt31923 2018-04-16 12:51
浏览 80

比较来自不同来源的数组并按来源划分值

I have some difficulties to get some good idea how to divide values from arrays. For example I have 3 arrays of URLs:

$urlsFromA = ['http://www.test.com', 'http://www.example.com', 'http://www.google.com', 'http://www.twitter.com'];
$urlsFromB = ['https://www.test.com', 'http://www.example.com', 'http://www.bing.com'];
$urlsFromC = ['http://www.test.com', 'http://www.google.com'];

I need to foreach all of the arrays and get to other arrays values which I have duplicates in these start arrays and remove from start arrays if they occurred in the others. It is a little hindrance. Because URLs can be difference by 'https' for example but I must treat them like they are the same. So my result will be arrays with URLs named as a Source of this URLs:

$urlsFromABC = ['http://www.test.com'];
$urlsFromAB = ['http://www.example.com'];
$urlsFromAC = ['http://www.google.com'];
$urlsFromBC = [];

$dataFromA = ['http://www.twitter.com'];
$urlsFromB = ['http://www.bing.com'];
$urlsFromC = []; 

In $urlsFromABC I have URL which was in all arrays on start (despite the fact that it is different because of 'https'). In starting arrays ($dataFromA ,$urlsFromB, $urlsFromC) should be URLs that are not duplicated in other start arrays. Maybe someone have idea how can I do this?

  • 写回答

1条回答 默认 最新

  • dp9599 2018-04-16 13:15
    关注

    First you need to create a function that changes https with http like this:

    function replace($arr){
        foreach($arr as $key => $ar){
            $ar = str_replace('https', 'http', $ar);
            $arr[$key] = $ar;
        }
        return $arr;
    }
    

    You will call this function like this:

    $urlsFromA = replace($urlsFromA);
    $urlsFromB = replace($urlsFromB);
    $urlsFromC = replace($urlsFromC);
    

    After that you can play with this two functions: array_diff and array_intersect.

    For example array_intersect:

    $urlsFromABC = array_intersect($urlsFromA, $urlsFromB, $urlsFromC);
    

    This will return all the values that exists in all the arrays. You can put how many arrays you want.

    And array_diff:

    $urlsFromA = array_diff($urlsFromA, $urlsFromB, $urlsFromC);
    

    This will return the values that are in the first array and not in other. You can put how many arrays you want.

    You can try how many cases you want. Hope it helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比