PHP中执行此操作的代码:
$string = '123,456,789,102'
$value = '564'
如何拆分$string
为数组,然后将数组中的每个值与$ value串联为二维数组,因此最终结果为:
$result = (
(564,123),
(564, 456),
(564, 789),
(564, 102)
)
因此,$ result可以用于在使用PDO的mysql中插入多行:
$stmt = $this->pdo->prepare('INSERT INTO tbl_user (id, bought) VALUES ((?,?),
(?,?),
(?,?),
(?,?),
(?,?),
)');
$stmt->execute($result);
注意:id和购买的列是varchar
//爆炸并存储$string
到数组中。数组包含$string
为字符串。
$s_array = explode(",", $string);
//to remove spaces
$spaces=array(); //to store space
$others=array(); //to store characters
foreach($s_array as $word)
{
if($word=='')
{
array_push($spaces,$word); //spaces are stored into space array
}
else
{
array_push($others,$word); //string variables are stored into others array
}
}
$count=count($others);
现在使用for循环。
for($i=0;$i<$count;$i++)
{
for($j=0;$j<$count;$j++)
{
$result[$i][$j] = $value;
$result[$i][$j+1] = $others[$i];
}
}
如果要将字符串数组转换为整数,请执行以下操作...
$integerarray = array_map('intval', array_filter($others, 'is_numeric'));
foreach($integerarray as $var)
{
array_push($result, array($value, $var) );
}
并进行编码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句