Jump to content

PHP - how to compare and eliminate repeated data set of arrays?


Recommended Posts

Hi,

 

please help me to solve my query... I was incurring a problem in executing a PHP code which executes 'n' number of data sets..

 

For example -- in the below shown outputs, I need to eliminate executing the 4th output which is similar (repeated data) to the 1st output,

 

input                     output

4,4,5,6,5               4,4,5,5,6

4,4,5,7,5               4,4,5,5,7

4,4,5,7,6               4,4,5,6,7

4,4,6,5,5               4,4,5,5,6

 

Kindly send me the function details to execute it without repetition.

 

Thanks a lot for your support!

 

Regards,

Maanu

How are we to do that when you haven't provided any code as to what you are doing now? Are you getting the data from a database, getting it from an external source, generating it yourself? All of these scenarios would require a different type of solution.

Thanks for your response.... I tried the following code but i executes only the partial set of data ... The data is given by myself.....  

 

function $dataset($mydataset , $array)

{

$array = array();

sort($mydataset);

$data = array_unique($mydataset);

foreach($array as $arr)

{

sort($arr);

if(count(array_intersect($mydataset,$array)) == count($arr))

return true;

}

}

Per the manual:

 

 

NoteNote that array_unique() is not intended to work on multi dimensional arrays.

 

In your function, I don't understand why you have a parameter for $array, but on the very first line you then define $array as an empty array - thereby overwriting any data that may have been passed in the function. I'm sure there are better solutions, but one approach would be to implode the data, sort it, then explode it back out.

This seems to work for your scenario

 

function filterMyDataset($mydataset)
{
    //Create temp array to remove non-unique values and sort
    $tempArray = array();
    foreach($mydataset as $subArray)
    {
        sort($subArray);
        $tempArray[] = implode(',', $subArray);
    }
    sort($tempArray);
 
    //Convert back to sub arrays for output
    $outputArray = array_unique($tempArray);
    foreach($outputArray as &$subArray)
    {
        $subArray = explode(',', $subArray);
    }
    return $outputArray;
}

Thanks for your response.... I tried the following code but i executes only the partial set of data ... The data is given by myself.....  

 

function $dataset($mydataset , $array)

{

$array = array();

sort($mydataset);

$data = array_unique($mydataset);

foreach($array as $arr)

{

sort($arr);

if(count(array_intersect($mydataset,$array)) == count($arr))

return true;

}

}

 

 

This seems to work for your scenario

function filterMyDataset($mydataset)
{
    //Create temp array to remove non-unique values and sort
    $tempArray = array();
    foreach($mydataset as $subArray)
    {
        sort($subArray);
        $tempArray[] = implode(',', $subArray);
    }
    sort($tempArray);
 
    //Convert back to sub arrays for output
    $outputArray = array_unique($tempArray);
    foreach($outputArray as &$subArray)
    {
        $subArray = explode(',', $subArray);
    }
    return $outputArray;
}

Thanks for your reply...

 

 

The dataset was already sorted in order, so thing is that only i need remove the repeated dataset to occur only once... The datas has to be echo only at the end of program... so implode and explode i cant use it correctly...

 

The function i need is that only to check duplicate dataset and in return it should delete it and occur only one set of data....

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.