#1
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,799
    Rep Power
    529

    Best way to store a unique list of strings in an array


    How would you recommend adding, renaming, and deleting a unique list of strings in an array?

    My application is storing a list of URLs of recent files uploaded by the user in a session.

    My first thought was simply to store them in a normal unassociated array:

    PHP Code:
    $path="http://someDomain.com/image.png";

    //Add.  Probably should first check if value exists in list.
    $_SESSION['mySession'][]=$path;

    //Delete
    $_SESSION['mySession']=array_filter($_SESSION['mySession'],function($val) use ($path) {return $val!=$path;});

    //Rename.  Haven't figured out yet, but expect a function like array_filter() 
    I then started thinking that I could instead store the data in the array's keys and not the array's values. Sure makes life easier. I've never stored data this way, and wonder if it poses any problems?

    PHP Code:
    //Add
    $_SESSION['mySession'][$path]=1;

    //Delete
    unset($_SESSION['mySession'][$path]);

    //Rename
    $_SESSION['mySession'][$new_path]) = $_SESSION['mySession'][$path]);
    unset(
    $_SESSION['mySession'][$path]); 
  2. #2
  3. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,660
    Rep Power
    4123
    I do. I can then store something else in the value section, like meta data (as a sub array), or the number of times it appeared (just be incrementing the value based on key)

    However, if your unique values contain characters or data that is not allowed as an array key then you may need to hash the value first (use as key), and store the original in the array element's value.

    doing
    PHP Code:
    $arr[$key]++; //gives it some value that will evaluate to a boolean true when required

    //is far faster then

    if(in_array($key,$arr)) {
         
    $arr[] = $key;

    retrieving is also quicker:
    PHP Code:
    if($arr[$key]) {

    }

    //vs

    if(in_array($key,$arr)) {


    I love PHP's loose typing!!!!
    Last edited by Northie; October 8th, 2013 at 11:29 AM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,911
    Rep Power
    1045
    Hi,

    Originally Posted by NotionCommotion
    I've never stored data this way, and wonder if it poses any problems?
    No, it doesn't create any problems. To the contrary, it's the correct solution if you need fast lookups. An array would be a terrible choice, because every single operation requires looping through the elements until you've found the right one.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    I then started thinking that I could instead store the data in the array's keys
    Most languages support data structures called "sets" for storing unique data. Though PHP lacks distinct type for set, however its arrays are very funny and handy structures and storing anything as keys is exactly the way to implement set.

    If you refer to manual on php arrays you'll see they could be used as hashtables - and hashset is just a hashtable for which we are not interested in values, but only in keys.

    So it is quite all right to my taste.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,799
    Rep Power
    529
    Thanks guys. I don't know why I doubted myself. Guess I was just surprised that I hadn't come across this sooner.

IMN logo majestic logo threadwatch logo seochat tools logo