Solved

php get duplicate record from an array

Posted on 2013-01-29
7
209 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamic Dropdowns 15 32
How to fix Datetime in MySQL? 4 47
jQuery Toggle & Anchor Links 5 42
Undefined variable problem 5 21
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
The viewer will learn how to dynamically set the form action using jQuery.

790 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