Search code examples
phparraysforeachcodeigniter-2

PHP, array manipulation to get keys with all same name


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:

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...


Solution

  • 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():

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