如何在foreach php中传递多个变量

时间:2018-03-23 16:45:09

标签: php arrays foreach

我想在foreach循环中传递多个变量,以将$ array_sma []中的值添加到我的数据库中。但到目前为止,我只能插入$ short_smas中的值,而我还想插入$ mid_smas中的值。我已经尝试过嵌套的foreach,但它正在增加值。

$period = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

$sma = array(6,9);

foreach ($sma as $range)    {


$sum = array_sum(array_slice($period, 0, $range));

$result = array($range - 1 => $sum / $range);


for ($i = $range, $n = count($period); $i != $n; ++$i) {

        $result[$i] = $result[$i - 1] + ($period[$i] - $period[$i - $range]) / $range;

}

$array_sma[] = $result;

}

list($short_smas,$mid_smas)=$array_sma;

foreach ($short_smas as $short_sma) {

$sql = "INSERT INTO sma (short_sma) 

  VALUES ('$short_sma') ";

if ($con->query($sql) === TRUE) {

    echo "New record created successfully<br><br>";

} else {

    echo "Error: " . $sql . "<br>" . $con->error;

}

}

我的问题中的代码工作正常,即$ array_sma []的第一个子数组($ short_smas)的值被插入到我的msql数据库的列short_sma中。我遇到的问题是当我尝试在我的数据库调用mid_sma的第二列中从$ array_sma []插入第二个子数组$ mid_smas(请参阅list())时。

我认为这与我想要实现的内容相关,但仍未在数据库中插入任何内容,来源:php+mysql: insert a php array into mysql

我没有任何mysql语法错误。

$array_sma[] = $result;

 $sql = "INSERT INTO sma (short_sma, mid_sma) VALUES ";
foreach ($array_sma as $item) {
 $sql .= "('".$item[0]."','".$item[1]."'),";
}

$sql = rtrim($sql,",");

1 个答案:

答案 0 :(得分:0)

问题$short_smas$mid_smas具有不同的大小。此外,它们是关联数组,因此要么从两者中选择唯一键,要么只允许一个值可用的键的空值,或者只选择两个数组中存在的键。下面的代码提供了第一个解决方案。

// first lets pick unique keys from both arrays
$uniqe_keys = array_unique(array_merge(array_keys($short_smas), array_keys($mid_smas)));
// alternatively we can only pick those present in both
// $intersect_keys = array_intersect(array_keys($short_smas),array_keys($mid_smas));

// now lets build sql in loop as Marcelo Agimóvel sugested
// firs we need base command:
$sql = "INSERT INTO sma (short_sma, mid_sma) VALUES ";

// now we add value pairs to coma separated list of values to
// insert using keys from prepared keys array
foreach ($uniqe_keys as $key) {
    $mid_sma = array_key_exists($key, $mid_smas)?$mid_smas[$key]:"";
    $short_sma = array_key_exists($key, $short_smas)?$short_smas[$key]:"";
    // here we build coma separated list of value pairs to insert
    $sql .= "('$short_sma', '$mid_sma'),";
}
$sql = rtrim($sql, ",");

// with data provided in question $sql should have string:
// INSERT INTO sma (short_sma, mid_sma) VALUES, ('3.5', ''), ('4.5', ''), ('5.5', ''), ('6.5', '5'), ('7.5', '6'), ('8.5', '7'), ('9.5', '8'), ('10.5', '9'), ('11.5', '10'), ('12.5', '11')

// now we execute just one sql command
if ($con->query($sql) === TRUE) {
    echo "New records created successfully<br><br>";
} else {
    echo "Error: " . $sql . "<br>" . $con->error;
}

// don't forget to close connection

MarceloAgimóvel还建议不要使用像这样的多个插页:

INSERT INTO tbl_name (a,b,c) VALUES (1,2,3);

最好使用单一:

INSERT INTO tbl_name
    (a,b,c)
VALUES
    (1,2,3),
    (4,5,6),
    (7,8,9);

这就是我在$sql循环中将值对附加到foreach并在循环外执行查询的原因。

另外值得一提的是,不是直接执行sql,而是使用预处理语句,因为它们不太容易被sql注入。