循环两个数组,从第一个数组一次访问一个,从第二个数组一次访问三个数组


Loop two arrays and access one-at-a-time from first array and three-at-a-time from the second array

我有两组数据,它们位于两个不同的数组中,我需要将它们组合成组,然后将结果插入我的数据库。

// name values
$arr=['Ram','joy','Rahul','Monty'];
// values that need to be split between names
$code=['10','11','12','13','14','15','16','17','18','19','20','21'];

在上面,我有两组数据。$arr包含name$code包含数字code

在这里,我需要根据用户输入将每个名称的代码插入到表中。假设$userinput=2前 2 个代码将在两行中插入名称Ram,然后后两个代码将插入名称joy依此类推,如果$userinput=3,它将相应地插入。下面给出了我对$userinput=3的预期输出。

_________________
 id   name   code
---+--------+-----
1  | Ram    | 10
---+--------+-----
2  | Ram    | 11
---+--------+-----
3  | Ram    | 12
---+--------+-----
4  | Joy    | 13
---+--------+-----
5  | Joy    | 14
---+--------+-----
6  | Joy    | 15
---+--------+-----
7  | Rahul  | 16
---+--------+-----
8  | Rahul  | 17
---+--------+-----
9  | Rahul  | 18
---+--------+-----
10 | Monty  | 19
---+--------+-----
11 | Monty  | 20
---+--------+-----
12 | Monty  | 21
---+--------+-----

我尝试过的另一个答案如下。

$arr1=['Ram','joy','Rahul','Monty'];
$code1=['10','11','12','13','14','15','16','17','18','19','20','21'];   
$userInput = 3;
$index = 0;
for($i=0;$i<count($arr1);$i++)
{
    $count = 0;
    echo $arr1[$i]."<br>";
    for($j=0;$j<count($code1);$j++)
    {
        if($count != $userInput)
        {
            $key = $j+$index;
           if(array_key_exists($key,$code1))
           {
               //echo $code1[$j+$index]."<br>";
               echo "INSERT INTO sampletable (`name`, `code`) VALUES (".$arr1[$i].", ".$code1[$key].")" . "<br />";
               $count++;
           }
        }
        else
        {
            $index += $userInput;
            break;
        }
    }
}

输出

Ram
INSERT INTO sampletable (`name`, `code`) VALUES (Ram, 10)
INSERT INTO sampletable (`name`, `code`) VALUES (Ram, 11)
INSERT INTO sampletable (`name`, `code`) VALUES (Ram, 12)
joy
INSERT INTO sampletable (`name`, `code`) VALUES (joy, 13)
INSERT INTO sampletable (`name`, `code`) VALUES (joy, 14)
INSERT INTO sampletable (`name`, `code`) VALUES (joy, 15)
Rahul
INSERT INTO sampletable (`name`, `code`) VALUES (Rahul, 16)
INSERT INTO sampletable (`name`, `code`) VALUES (Rahul, 17)
INSERT INTO sampletable (`name`, `code`) VALUES (Rahul, 18)
Monty
INSERT INTO sampletable (`name`, `code`) VALUES (Monty, 19)
INSERT INTO sampletable (`name`, `code`) VALUES (Monty, 20)
INSERT INTO sampletable (`name`, `code`) VALUES (Monty, 21)

这里是...

我希望这对你有所帮助...我不在我的笔记本电脑上,所以这是我能做的。

    <?php
    $user_input = 3; // this should be your user input.
    $arr=['Ram','joy','Rahul','Monty'];
    $codes=['10','11','12','13','14','15','16','17','18','19','20','21'];
    $counter1=0; //counter for your $codes to continue.
    foreach($arr as $name){
        $counter = 0; //counter for your condition...
        for($x=0; $x <= count($codes); $x++){
            $counter++;
            if($counter <= $user_input){
                echo "INSERT INTO sampletable (`name`, `code`) VALUES ($name, $codes[$counter1])"."<br />";
            }else{
                break;
            }
            $counter1++;
        }
    }

我认为这就是您要查找的内容,假设您使用PDO作为数据库连接。

for ($i = 0; $i <= count($code); $i++) {
  $db->query("INSERT INTO db_code (name, code) VALUES ('{$arr[$i]}', '{$code[$i]}'"); 
}

当然,如果您尚未设置$db功能或handler,只需添加:

$db = new PDO("mysql:host=DATABASE_HOST;dbname=DATBASE_NAME", DATABSE_USERNAME, DATABASE_PASSWORD);

对于大多数用户来说,host = localhost, name = your databse to enter to, username = root, password = nothing

.

如果您想了解有关PDO的更多信息,请参阅此处

下面是一个 sql 语句解决方案:

$arr    =   array('Ram','joy','Rahul','Monty');
$code   =   array('10','11','12','13','14','15','16','17','18','19','20','21');
function qAssemble($arr,$code)
    {
        $split  =   array_chunk($code,ceil((count($code) / count($arr))));
        $new    =   array();
        foreach($arr as $k=>$name) {
            $b      =   array_filter($split[$k]);
            $a      =   array_fill(0,count($b),$name);
            $new    =   array_merge($new,array_map(function($v) use ($name)
                {
                    return "('{$name}','{$v}')";
                },$b));
        }
        return implode(',',$new);
    }
$str    =   "insert into `db_code` (`name`, `code`) VALUES ".qAssemble($arr,$code);
print_r($str);

为您提供:

insert into `db_code` (`name`, `code`) VALUES ('Ram','10'),('Ram','11'),('Ram','12'),('joy','13'),('joy','14'),('joy','15'),('Rahul','16'),('Rahul','17'),('Rahul','18'),('Monty','19'),('Monty','20'),('Monty','21')

试试这个:

        $arr = ['Ram', 'joy', 'Rahul', 'Monty'];
        $code = ['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21'];
        $userInput = 3;
        $new_code = array_chunk($code, $userInput);
        $final = array_combine($arr, $new_code);
        foreach ($final as $name => $code_array) {
            echo "INSERT INTO sampletable (`name`, `code`) VALUES ($name, $code_array[0]]), ($name, $code_array[1]]), ($name, $code_array[2]])" . "<br />";
        }