PHP,数组操作,获取名称相同的键

时间:2023-02-09 21:37:22

I have this array posting to my controller:

我有这个数组张贴到我的控制器:

Array
(
    [id] => Array
        (
            [0] => 95
            [1] => 69
        )
)

I want:

我想要:

Array(

    [id] => 95
    [id] => 69
)

As I am using CodeIgniter's $this->db->delete() function and it takes the array key value as the column for the WHERE clause. I have this code at the moment:

当我使用CodeIgniter的$this->db->delete()函数时,它将数组键值作为WHERE子句的列。我现在有这个代码:

foreach($ids as $k => $v){

    $formatIds['id'] = $v;

}

Which just gives me one of the rows and not the rest.

它只给我一行而不是其余的行。

I then tried:

然后我尝试:

foreach($ids as $k => $v){

    $formatIds['id'][] = $v;

}

But this gives me a MultiDimensional array...

但这给了我一个多维数组……

1 个解决方案

#1


4  

The answer to your question is "not possible": array keys must always be unique.

问题的答案是“不可能”:数组键必须始终是唯一的。

The answer to what you're trying to do is to use where_in():

您要做的答案是使用where_in():

$names = array(95,69);
$this->db->where_in('id', $names);
$this->db->delete('mytable');

#1


4  

The answer to your question is "not possible": array keys must always be unique.

问题的答案是“不可能”:数组键必须始终是唯一的。

The answer to what you're trying to do is to use where_in():

您要做的答案是使用where_in():

$names = array(95,69);
$this->db->where_in('id', $names);
$this->db->delete('mytable');