Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

Compare two multidimensional arrays & Get difference & unique values in two different arrays PHP

I have two multidimensional arrays with me..

$a1= Array
(
    [0] => Array
        (
            [sight_id] => 13
            [location] => Jodhpur, Rajasthan, India

        )

    [1] => Array
        (
            [sight_id] => 14
            [location] => Jodhpur Jn, Jodhpur, Rajasthan, India
                  )

    [2] => Array
        (
            [sight_id] => 15
            [location] => D-Kirtinagar, Jodhpur, Rajasthan, India

        )

   );


$a1= Array
(
    [0] => Array
        (
            [sight_id] => 13
            [location] => Jodhpur, Rajasthan, India

        )

    [1] => Array
        (
            [sight_id] => 14
            [location] => Jodhpur Jn, Jodhpur, Rajasthan, India
                  )

       [2] => Array
        (
            [sight_id] => 16
            [location] => Jaisalmer, Rajasthan, India

        )

    [3] => Array
        (
            [sight_id] => 17
            [location] => Fort Road, Amar Sagar Pol, Jaisalmer, Rajasthan, India

        ));

I want to split above given arrays into three different arrays..

$intersect_array=Array
(
    [0] => Array
        (
            [sight_id] => 13
            [location] => Jodhpur, Rajasthan, India

        )

    [1] => Array
        (
            [sight_id] => 14
            [location] => Jodhpur Jn, Jodhpur, Rajasthan, India
                  ));

$only_a1=Array(
[0]=> Array
        (
            [sight_id] => 15
            [location] => D-Kirtinagar, Jodhpur, Rajasthan, India

        ));
$only_a2=Array(
[1] => Array
        (
            [sight_id] => 16
            [location] => Jaisalmer, Rajasthan, India

        )

    [2] => Array
        (
            [sight_id] => 17
            [location] => Fort Road, Amar Sagar Pol, Jaisalmer, Rajasthan, India

        ));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Convert arrays to a format, where array index is the sight_id:

$b1 =array();
foreach($a1 as $x)
    $b1[$x['sight_id']] = $x['location'];

$b2 =array();
foreach($a2 as $x)
    $b2[$x['sight_id']] = $x['location'];

Calculate the differences and intersection:

$c_intersect = array_intersect_key($b1,$b2);
$c_1 = array_diff_key($b1,$b2);
$c_2 = array_diff_key($b2,$b1);

Convert arrays back to your format:

$intersect_array = array();
foreach($c_intersect as $i=>$v)
    $intersect_array[] = array('sight_id'=>$i,'location'=>$v);

$only_a1 = array();
foreach($c_1 as $i=>$v)
    $only_a1[] = array('sight_id'=>$i,'location'=>$v);

$only_a2 = array();
foreach($c_2 as $i=>$v)
    $only_a2[] = array('sight_id'=>$i,'location'=>$v);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...