2

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

4
  • Why do you want a broken array? It makes no sense for two different values to have the same key. I think you should change your approach. Commented Mar 18, 2015 at 16:38
  • 2
    This is not possible, because it is logically senseless. It contradicts the basic idea of an associative array. Commented Mar 18, 2015 at 16:39
  • 2
    Array( [0] => 95 [1] => 69 ) is possible; but you can't have multiple array elements with the same key Commented Mar 18, 2015 at 16:40
  • ok so if I try using the first array i posted above in the CodeIgniter function to delete rows it tries something like this in the query DELETE FROM table WHERE 0 = 95, DELETE FROM table WHERE 1 = 69.... So how can I achieve what I want Commented Mar 18, 2015 at 16:44

1 Answer 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():

$names = array(95,69);
$this->db->where_in('id', $names);
$this->db->delete('mytable');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.