开发者

php looping two arrays

开发者 https://www.devze.com 2023-02-04 19:26 出处:网络
$arr1 = array( 1 => array( 30 => 100 31 => 800 ), 2 => array( 30 => 200 31 => 900 ), 3 => array(
$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) 开发者_如何学PythonVALUES ('31','4',110);
INSERT INTO tablename (option_value_id,duration_id,price) VALUES ('31','5',120);    

and so on. How can i do this?


  foreach ($arr2 as $id)
  {
    foreach ($arr1 as $i => $v)
    {
        query("insert into tablename (option_value_id,duration_id,price)"
            . " values ('$id','$i',$v)");
    }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消