php - array_splice removing more than one item -


i have following method:

public function selectfinal(){     $db = new database();     for($i = 0; $i < 5; $i++){         $key_id = mt_rand(0, count($this->candidates) - 1);         $itm    = $this->candidates[$key_id];         $host   = $itm["host"];         $item   = $itm["item"];         $db->query("insert ignore trends (trend_id, host, item) values (?, ?, ?)", array($this->nextid, $host, $item));         array_splice($this->candidates, $key_id, -1);         print_r($this->candidates);         $this->nextid++;     } } 

for print_r() getting output:

array (     [0] => array         (             [host] => www.youtube.com             [item] => iytnbm8wa1c         )      [1] => array         (             [host] => www.youtube.com             [item] => kffacxfa7g4         )      [2] => array         (             [host] => www.youtube.com             [item] => kxyiu_jcytu         )      [3] => array         (             [host] => www.youtube.com             [item] => 7avhxe-ol-s         )      [4] => array         (             [host] => www.youtube.com             [item] => qkm6rjf15cg         )  ) array (     [0] => array         (             [host] => www.youtube.com             [item] => iytnbm8wa1c         )      [1] => array         (             [host] => www.youtube.com             [item] => qkm6rjf15cg         )  ) array (     [0] => array         (             [host] => www.youtube.com             [item] => qkm6rjf15cg         )  ) array (     [0] => array         (             [host] => www.youtube.com             [item] => qkm6rjf15cg         )  ) array (     [0] => array         (             [host] => www.youtube.com             [item] => qkm6rjf15cg         )  ) 

the array start 5 or more items in it. select random item array , insert database remove array. want 5 times 5 random items array. reason selecting 1 removing 3 items array, , not sure why (shown in second section of code).

edit: final working result

public function selectfinal(){     $db = new database();     for($i = 0; $i < 5; $i++){         $key_id = mt_rand(0, count($this->candidates) - 1);         $itm    = array_values(array_merge([$this->nextid], array_splice($this->candidates, $key_id, 1)[0]));         $db->query("insert ignore trends (trend_id, host, item) values (?, ?, ?)", $itm);         $this->nextid++;     } } 

you more safe in splicing element out , use outtake. in case made error that, notice not having correct values store. make more aware of potential problem:

$key_id = mt_rand(0, count($this->candidates) - 1); $itm = array_splice($this->candidates, $key_id, -1); var_dump($itm); 

see? can better pin-point problem, e.g. -1 not 1. see http://php.net/array_splice

public function selectfinal() {     $db = $this->db;      ($i = 0; $i < 5; $i++)      {         $key_id = mt_rand(0, count($this->candidates) - 1);         $values = array_merge(             [$this->nextid], array_splice($this->candidates, $key_id, 1)                                                                      ###         );          print_r($this->candidates);          $db->query(             "insert ignore trends (trend_id, host, item) values (?, ?, ?)",              array_values($values)         );          $this->nextid++;     } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -