array_splice删除多个项目

时间:2013-05-04 20:16:44

标签: php arrays

我有以下方法:

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 into trends (trend_id, host, item) values (?, ?, ?)", array($this->nextId, $host, $item));
        array_splice($this->candidates, $key_id, -1);
        print_r($this->candidates);
        $this->nextId++;
    }
}

对于print_r()我得到了这个输出:

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
        )

)

数组将以5个或更多项开头。我想要做的是从数组中选择一个随机项并将其插入数据库,然后将其从数组中删除。我想这样做5次从阵列中获取5个随机项目。但由于某种原因,它选择1然后从数组中删除3个项目,我不知道为什么(在代码的第二部分中显示)。

修改:最终工作结果

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 into trends (trend_id, host, item) values (?, ?, ?)", $itm);
        $this->nextId++;
    }
}

2 个答案:

答案 0 :(得分:1)

在拼接元素并使用该元素时,您更加安全。如果您使用该错误,您会注意到没有正确的值来存储。这将使您更加了解潜在的问题:

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

请参阅?然后,您可以更好地指出问题,例如-1不是1.请参阅http://php.net/array_splice

public function selectFinal() {
    $db = $this->db;

    for ($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 into trends (trend_id, host, item) values (?, ?, ?)", 
            array_values($values)
        );

        $this->nextId++;
    }
}

答案 1 :(得分:0)

如果您只想删除特定键的数组项,可以使用 -

unset($this->candidates[$key_id])
相关问题