Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_combine combine array values in foreach loop

I am Using foreach loop for array.

i have two set of arrays

i want to pass these two array from one foreach loop code:

//getting variables from form
$lists=$_POST['list'];
$assigndue=$_POST['assign'];

//foreach loop
foreach (array_combine($lists,$assigndue) as $listitem => $due)
{
 $qqu="INSERT INTO `todolist`(`todoid`,`listdetail`,`assign`,`order`)VALUES('$todoidd','$listitem','$due','$o')";
        $ins=mysql_query($qqu);
}

every thing is working fine but i have face an issue when array is like that >>

Array
(
    [0] => s
    [1] => s
    [2] => s
    [3] => a
)

its return this queries

INSERT INTO `todolist`(`todoid`,`listdetail`,`assign`,`order`)VALUES('8','s','Select Email','1') 

INSERT INTO `todolist`(`todoid`,`listdetail`,`assign`,`order`)VALUES('8','a','Select Email','2') 

its combine the 's' in the array .

i dont know what to do next please help me to do it.


1 Answers

I think array_combine will just get only unique value.

Why don't you do have to combine it anyway? just foreach the $lists as $index => $listitem and then in foreach loop, just add $due = $assigndue[$index] It would work fine, if i understand your program logic correctly.

One more thing that it needs to be mentions is about optimization. You should NOT have multiple query of each insert to database. Just combine them in to single execution in following format,

INSERT INTO todolist (COLUMNS) VALUES (ATTRIBUTE), (ATTRIBUTE)

By making insert value clause as ARRAY and then use join(ARRAY) for the values clause in the query.

If you need any help in detail, please ask.

like image 108
chaintng Avatar answered Jul 18 '26 22:07

chaintng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!