php - Loop two arrays and access one-at-a-time from first array and three-at-a-time from the second array
Get the solution ↓↓↓I have two sets of data which are in two different arrays and I need to combine them in groups then insert the results into my database.
{-code-1}
In above I have two set of data.{-code-2}
contains the{-code-3}
and{-code-4}
contains a numericcode
.
Here I need to insert the code per {-code-3} into the table as per the user input. Suppose$userinput=2
the first 2 code will insert for {-code-3}Ram
in two row, then second two will insert for {-code-3}joy
and so on and if$userinput=3
it will insert accordingly. My expected output for$userinput=3
is given below.
_________________
id {-code-3} 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
---+--------+-----
Answer
Solution:
Another I have tried answer is below.
$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;
}
}
}
Output
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)
Answer
Solution:
here it is...
I hope this helps you... I'm not on my laptop so this is what I could do..
<?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++;
}
}
Answer
Solution:
I think this is what you're looking for, assuming you're using PDO as your DataBase connection.
for ($i = 0; $i <= count($code); $i++) {
$db->query("INSERT INTO db_code (name, code) VALUES ('{$arr[$i]}', '{$code[$i]}'");
}
Of course, if you haven't set up the$db
function, or ahandler
, just add:
$db = new PDO("mysql:host=DATABASE_HOST;dbname=DATBASE_NAME", DATABSE_USERNAME, DATABASE_PASSWORD);
And for most users, thehost = localhost, name = your databse to enter to, username = root, password = nothing
.
If you want more information on PDO, see here
Answer
Solution:
Here is a one-sql statement solution:
$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);
Gives you:
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')
Answer
Solution:
Try this :
$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 />";
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.