小编典典

PHP数组到SQL

sql

$array = array
        (
            53 => array('num' => 20,'name' => 'aaa'),
            10 => array('num' => 20,'name' => 'bbb')
        );

$ sql =“ INSENT INTO data(id,num,name)VALUES
('53','20','aaa'),('10','20','bbb')”;

如何将$ array转换成('53','20','aaa'),('10','20','bbb')

谢谢


阅读 222

收藏
2021-04-22

共1个答案

小编典典

implode()可以帮助您做到这一点,尽管您将需要遍历并将其应用于每个单独的数组:

 $resultStrings = array();
 foreach ($array as $key => $values) {
    $subarrayString = "('$key','" . implode($values, "','") . "')";
    $resultStrings[] = $subarrayString;
 }

 $result = implode($resultStrings, ",");
2021-04-22