Jump to content

Remove double entry in array?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Hello,
I am working on script which connect to a router and after that I want to delete double entry in array.
EX:

This output array:
Array
(
   [0] => Array
      (
         [.id] => *1
         [name] => ali
      )

   [1] => Array
      (
         [.id] => *2
         [name] => ali
      )

   [2] => Array
      (
         [.id] => *3
         [name] => ali

      )
) 

I want to delete both ali?
anyone have optimized code for this job?

Edited by Alexander, 15 April 2011 - 03:28 PM.
(Code tags to preserve print_r output)


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Being a multidimensional array, you must iterate through the list or employ a callback function to array_filter. The two should perform nearly the same efficiency wise, so that is not of issue.

<?php

$sampledata = array(
                array(".id" => "*1", "name" => "ali"),
                array(".id" => "*2", "name" => "ali"),
                array(".id" => "*3", "name" => "ali"),
                array(".id" => "*4", "name" => "john"),
                array(".id" => "*5", "name" => "john"),
              );


function array_filter_callback($array) {
    global $names;
    $name = $array['name'];
    if(isset($names[$name])) {
        return false;
    } else {
        $names[$name] = 1;
        return true;
    }
}

$names = array();

$filtered_array = array_filter($sampledata, "array_filter_callback");
print_r($filtered_array);
Array
(
    [0] => Array
        (
            [.id] => *1
            [name] => ali
        )

    [3] => Array
        (
            [.id] => *4
            [name] => john
        )

)
This will essentially store all names in a global array, if the name is already in the array then the entry will be filtered out (by returning false to array_filter's callback result.)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
Good,
But I need one more thing,
I want to use this script for API and I need to know where should I put API command to remove user!
Before return false; or return false; ????


#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Yes, before the "return false;" would be the appropriate place to handle if the user is already in the array. My array_filter callback will strip out duplicates in the actual array, is this required, or do you only need to find a duplicate and use the API to remove them?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I want to use API to don't allow user duplicate login.Then I need to check duplicates username.

#6
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
Sorry if this is OOT and may sounds silly, but why ".id" instead of "id" and "*1" instead of just "1"? Since I am new in PHP I have the impression that these have important role (like php best practice?).

TIA

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

LuthfiHakim said:

Sorry if this is OOT and may sounds silly, but why ".id" instead of "id" and "*1" instead of just "1"? Since I am new in PHP I have the impression that these have important role (like php best practice?).

TIA
I've not seen that either in general practice, so it's something either from another system or made up to be special as far as I can tell...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users