Array_combine concatenates array values ​​in foreach loop

I am using a foreach loop on an array.

i has two sets of arrays

I want to pass these two arrays from one foreach loop the 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 works fine, but i have a problem where array is like this ->

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

      

returns these requests

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') 

      

it is concatenated by 's' in an array.

I don't know what to do next, please help me to do this.

+3


source to share


3 answers


I think array_combine will only get a unique value.

Why don't you combine it? just foreach

$lists as $index => $listitem

and then in a loop foreach

just add $due = $assigndue[$index]

This will work fine if I understand your programming logic correctly.

Another thing to mention is optimization. You should NOT have multiple queries for each database insert. Just combine them into one performance in the following format,



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

By making the insert value clause as ARRAY and then use join(ARRAY)

for the values ​​clause in the query.

If you need any help, please ask.

+1


source


Since it array_combine

will use the elements of the first array as keys for the resulting array and because the keys must be unique, you cannot use array_combine

if $lists

there are several identical elements.

But it shouldn't be a problem to iterate over both arrays yourself:



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

//for loop
for ($i = 0; $i < count($lists) && $i < count($assigndue); $i++)
{
 $qqu="INSERT INTO `todolist`(`todoid`,`listdetail`,`assign`,`order`)VALUES('$todoidd','" . $lists[$i] . "','" . $assigndue[$i] . "','$o')";
        $ins=mysql_query($qqu);
}

      

Just make sure both arrays are the same size.

0


source


Array keys must be unique, so passing s, s, s, since keys to array_combine will just give you s and entries

Instead of using array_combine, consider using SPLs MultipleIterator:

$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($lists), 'listitem');
$mi->attachIterator(new ArrayIterator($assigndue), 'due');
foreach($mi as $details) {
    extract($details);
    $qqu = ....
}

      

Or if you are using PHP> = 5.5

$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($lists));
$mi->attachIterator(new ArrayIterator($assigndue));
foreach($mi as list($listitem, $due)) {
    $qqu = ....
}

      

0


source







All Articles