Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php get duplicate record from an array

Posted on 2013-01-29
7
Medium Priority
?
223 Views
Last Modified: 2013-02-01
i have following array which could have duplicate records w.r.t [rt] & [userList] .
It has to be clubbed into one in [rc] and [ri].
Over here boisoc.com and  lovetomarry.com has same [rt] & [userList] value hence there o/p should be clubbed to one .

Array
(
    [0] => Array
        (
            [rt] => doesnt contain
            [rc] => @boisoc.com
            [userList] => admin@mangalsutrabandhan.in,abc@mangalsutrabandhan.in

            [ri] => 0
        )

    [1] => Array
        (
            [rt] => doesnt contain
            [rc] => @lovetomarry.com
            [userList] => admin@mangalsutrabandhan.in,abc@mangalsutrabandhan.in

            [ri] => 1
        )
   [2] => Array
        (
            [rt] => contain
            [rc] => @lovetomarry.com
            [userList] => admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in

            [ri] => 2
        )
  [3] => Array
        (
            [rt] => contain
            [rc] => @mangalsutrabandhan.com
            [userList] => admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in,xyz@mangalsutrabandhan.in

            [ri] => 3
        )

)

o/p
Array
(
    [0] => Array
        (
            [rt] => doesnt contain
            [rc] => @boisoc.com,lovetomarry.com
            [userList] => admin@mangalsutrabandhan.in,abc@mangalsutrabandhan.in

            [ri] => 0,1
        )

   
   [1] => Array
        (
            [rt] => contain
            [rc] => @lovetomarry.com
            [userList] => admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in

            [ri] => 2
        )
   [2] => Array
        (
            [rt] => contain
            [rc] => @mangalsutrabandhan.com
            [userList] => admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in,xyz@mangalsutrabandhan.in

            [ri] => 3
        )

)
0
Comment
Question by:Insoftservice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38830631
Perhaps you would want to start with usort()?  I am not sure I understand the output you would expect to get from the process, but if you want to manually create the output you expect and you can post it here, I will be glad to look for a process to make this input into your desired output.  Best regards, ~Ray
0
 
LVL 15

Author Comment

by:Insoftservice
ID: 38830764
o/p is already there

o/p
Array
(
    [0] => Array
        (
            [rt] => doesnt contain
            [rc] => @boisoc.com,lovetomarry.com
            [userList] => admin@mangalsutrabandhan.in,abc@mangalsutrabandhan.in

            [ri] => 0,1
        )

   
   [1] => Array
        (
            [rt] => contain
            [rc] => @lovetomarry.com
            [userList] => admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in

            [ri] => 2
        )
   [2] => Array
        (
            [rt] => contain
            [rc] => @mangalsutrabandhan.com
            [userList] => admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in,xyz@mangalsutrabandhan.in

            [ri] => 3
        )

)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38833659
Thanks for that.  So it's a combination of [rt] and [userlist] that indicates an equal comparison?  And when we detect that, we want to set [rc] equal to a concatenation of all of the [rc] elements in all of the matching [rt] + [userlist] items?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Author Comment

by:Insoftservice
ID: 38833906
Exactly you caught the right nerve.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38834794
Please see: http://www.laprbass.com/RAY_temp_insoftservice.php

<?php // RAY_temp_insoftservice.php
error_reporting(E_ALL);
echo '<pre>';


// So it's a combination of [rt] and [userlist] that indicates an equal comparison?
// And when we detect that, we want to set [rc] equal to a concatenation of all of
// the [rc] elements in all of the matching [rt] + [userlist] items?


// TEST DATA FROM THE POST AT EE
$dat
=
Array
( Array
  ( 'rt' => 'doesnt contain'
  , 'rc' => '@boisoc.com'
  , 'userList' => 'admin@mangalsutrabandhan.in,abc@mangalsutrabandhan.in'
  , 'ri' => '0'
  )
, Array
  ( 'rt' => 'doesnt contain'
  , 'rc' => '@lovetomarry.com'
  , 'userList' => 'admin@mangalsutrabandhan.in,abc@mangalsutrabandhan.in'
  , 'ri' => '1'
  )
, Array
  ( 'rt' => 'contain'
  , 'rc' => '@lovetomarry.com'
  , 'userList' => 'admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in'
  , 'ri' => '2'
  )
, Array
  ( 'rt' => 'contain'
  , 'rc' => '@mangalsutrabandhan.com'
  , 'userList' => 'admin2@mangalsutrabandhan.in,test@mangalsutrabandhan.in,xyz@mangalsutrabandhan.in'
  , 'ri' => '3'
  )
)
;
// DOES IT LOOK RIGHT? (YES)
print_r($dat);

// ITERATE OVER THE DATA TO CREATE THE COMPARISON FIELD AND COLLECT THE TANK ELEMENTS
error_reporting(E_ALL ^ E_NOTICE);
$text_tank = array();
$numb_tank = array();
foreach ($dat as $key => $arr)
{
    $arr['rt_userList'] = $arr['rt'] . $arr['userList'];
    $text_tank[$arr['rt_userList']] .= ',' . $arr['rc'];
    $numb_tank[$arr['rt_userList']] .= ',' . $arr['ri'];
    $dat[$key] = $arr;
}
error_reporting(E_ALL);

// ITERATE OVER THE DATA TO TIDY UP THE FIELDS
foreach ($text_tank as $key => $str)
{
    $str = ltrim($str, ',');
    $str = str_replace(',@', ',', $str);
    $text_tank[$key] = $str;
}
foreach ($numb_tank as $key => $str)
{
    $numb_tank[$key] = ltrim($str, ',');
}

// REMOVE DUPLICATES FROM THE ORIGINAL DATA
foreach ($dat as $key => $arr)
{
    $new[$arr['rt_userList']] = $arr;
}

// SUPPLY REPLACEMENT FIELDS
foreach ($new as $key => $arr)
{
    $arr['rc'] = $text_tank[$key];
    $arr['ri'] = $numb_tank[$key];
    unset($arr['rt_userList']);
    $new[$key] = $arr;
}

// STRIP THE ASSOCIATIVE KEYS AND SHOW THE WORK PRODUCT
$new = array_values($new);
print_r($new);

Open in new window

Best regards, ~Ray
0
 
LVL 15

Author Closing Comment

by:Insoftservice
ID: 38842658
thx worked as expected
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38844257
Thanks for the points, ~Ray
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question