Solved

php get duplicate record from an array

Posted on 2013-01-29
7
207 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 109

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 109

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Author Comment

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

Accepted Solution

by:
Ray Paseur earned 500 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 109

Expert Comment

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

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php function to remove a file 31 43
php extract($_REQUEST) 5 56
000webhost.com default error log 1 28
Get a subdirectory name from a url 5 16
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

832 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