gpt4 book ai didi

PHP数组到SQL

转载 作者:可可西里 更新时间:2023-11-01 00:52:27 26 4
gpt4 key购买 nike

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

$sql ="插入数据(id,num,name) VALUES('53','20','aaa'),('10','20','bbb') ";

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

谢谢

最佳答案

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

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

$result = implode($resultStrings, ",");

关于PHP数组到SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4300552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com