Foreach is returning only one record

3

I have a problem with my code. I have the following code:

<?php
require('class/habeo.class.php');
$Habeo->DuplicateRegister('contatos', array('id'=>'1', 'id'=>'6'));
?>

This class duplicates the records I have in the database. It counts how many elements it has in the array and loopes to duplicate the id . The class that duplicates is as follows:

<?php
function($de, $condicao){   
    foreach($condicao as $campo=>$value){
        $SQL = mysql_query('INSERT INTO {$de} ({$Columns}) 
                                  SELECT {$Columns} FROM {$de} 
                                  WHERE {$campo}='{$value}');
    }
}
?>

The problem is that it only loops with 1, and there are 2 records in the array.

I have seen that the error is because the array indices are the same name id and so are returning only 1, now when I change the name it returns 2.     

asked by anonymous 06.07.2014 / 05:11

2 answers

4

As Bacco said , you can not have more than one key with the same name in an array. But you can call your function multiple times:

$Habeo->DuplicateRegister('contatos', array('id'=>'1'));
$Habeo->DuplicateRegister('contatos', array('id'=>'6'));

If the IDs are in another array, loop out:

$ids = array(1, 6);
foreach($ids as $id) {
    $Habeo->DuplicateRegister('contatos', array('id'=>$id));
}
    
06.07.2014 / 05:56
3

One option is to use an array:

array( array("id"=>1), array("id"=>2) )

And the loop would look like this:

foreach ($lista as $item) {
    $item["id"];
}
    
06.07.2014 / 17:15