Unique entries in an array

后端 未结 4 1881
小蘑菇
小蘑菇 2021-01-26 08:43

I have the following that stores the previous 10 URL\'s into a session:

function curPageURL() {
 $pageURL = \'http\';
 if ($_SERVER[\"HTTPS\"] == \"on\") {$pageU         


        
相关标签:
4条回答
  • 2021-01-26 09:15
    if(!in_array($CurrentPage, $_SESSION['pages']) {
        $_SESSION['pages'][] = $CurrentPage;
    }
    
    0 讨论(0)
  • 2021-01-26 09:24

    Could array_unique be of help?

    0 讨论(0)
  • 2021-01-26 09:28

    instead of $_SESSION['pages'][] = $CurrentPage try $_SESSION['pages'][$CurrentPage] = 1

    /edit: to keep items sorted, unset first:

     unset($_SESSION['pages'][$CurrentPage]);
     $_SESSION['pages'][$CurrentPage] = 1;
    
    0 讨论(0)
  • 2021-01-26 09:41

    Just after

    $_SESSION['pages'][] = $CurrentPage;
    

    you need to add

    $_SESSION['pages'] = array_unique($_SESSION['pages']);
    

    Docs are available here

    This method requires less processing, as it's a native function. Performing an 'if' on each item in the array could potentially be quite costly.

    0 讨论(0)
提交回复
热议问题