How to rearrange data in array so that two similar items are not next to each other?

后端 未结 8 1942
暗喜
暗喜 2020-12-31 12:00

Just want to rearrange the data in array so that similar items are not next to each. The data should not be removed from the array, if it can\'t be rearranged it can be put

8条回答
  •  渐次进展
    2020-12-31 12:06

    Populate your array into a class var, then you can run your custom sort methods on it without changing it. Congratulations, you just created a new nosql database.

    What is scaring everyone is losing the original order.

    This is why the key in a hash is called 'index', think about it.

    class Dingleberry
      {
    
       private $shiz= array();
    
        function __construct(array $a) { $this->shiz = $a; }
    
    ##### PUBLIC
    
        public function unmolested() { return $this->shiz; }
    
        /* @see http://www.php.net/manual/en/ref.array.php */
        public function sort($type)
            {
            switch ($type)
                {
                case 'key_reverse': return krsort($this->shiz); break;
                # Check all the php built in array sorting methods to 
                        # make sure there is not already one you want
                        # then build this out with your own
                }
            }
    
        
      
    

    }

提交回复
热议问题