0
$arr1 = array(
    1 => array(
        30 => 100
        31 => 800
    ),
    2 => array(
        30 => 200
        31 => 900
    ),
    3 => array(
        30 => 300
        31 => 100
    ),
    4 => array(
        30 => 400
        31 => 110
    ),
    5 => array(
        30 => 500
        31 => 120
    ),
    6 => array(
        30 => 600
        31 => 130
    ),
    7 => array(
        30 => 260
        31 => 140
    )
);

 

$arr2 = array(
    0 => array(
        id => 30  
    ),
    1 => array(
        id => 31          
    )
)

I need to loop through the $arr1 and insert in this fashion

INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('30','1',100);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('30','2',200);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('30','3',300);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('30','4',300);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('30','5',500);

and so on

INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('31','1',900);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('31','2',900);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('31','3',100);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('31','4',110);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('31','5',120);    

and so on. How can i do this?

1 Answer 1

3
  foreach ($arr2 as $id)
  {
    foreach ($arr1 as $i => $v)
    {
        query("insert into tablename (option_value_id,duration_id,price)"
            . " values ('$id','$i',$v)");
    }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

@user286389 Two remarks: 1. PHP is very convenient when working with arrays (foreach, all arrays are associative (maps)). 2. You can edit your profile and set a decent user name (I apologize if 286389 has actually a meaning, and is number chosen by yourself :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.