Solved

php get duplicate record from an array

Posted on 2013-01-29
7
201 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
  • 4
  • 3
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Author Comment

by:Insoftservice
Comment Utility
Exactly you caught the right nerve.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
thx worked as expected
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points, ~Ray
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now