Solved

php get duplicate record from an array

Posted on 2013-01-29
7
210 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 110

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 110

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 110

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 110

Expert Comment

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

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX Wordpress Not Reading Variable 2 56
Ajax and PHP 9 53
Use Select Query to Return Results as a Form 9 41
Integrating blogs between to separate WordPress websites 4 23
This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 look for a specific file type in a local or remote server directory using PHP.

735 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