我有一个这样的数组。这个数组值将是一个动态值。因此将会有一个从author0到authorN的数组(其中N表示任意数字)。订单将仅按以下顺序进行。
Array
(
[author0] => 31
[percent0] => 10
[start0] => 2014-05-20
[end0] => 2014-05-21
[author1] => 70
[percent1] => 20
[start1] => 2014-05-21
[end1] => 2014-05-21
[author2] => 75
[percent2] => 20
[start2] => 2014-05-21
[end2] => 2014-05-21
)
我需要将上述数组值存储在db中,如下所示
表名:author_percentage
author percentage start end
31 10 2014-05-20 2014-05-21
70 20 2014-05-21 2014-05-21
75 20 2014-05-21 2014-05-21
那我怎么才能做到呢?
谢谢,Kimz
添加一个临时数组:)
$tempArr = array();
foreach($array as $rows) {
$tempArr[] = $rows;
if(count($tempArr) == 4) {
$query = "INSERT INTO author_percentage ('author','precentage','start','end') VALUES ($tempArr[0],$tempArr[1],$tempArr[2],$tempArr[3])";
$tempArr = array();
}
}
我会这样做
$i = 0;
while(array_key_exists("author". $i, $array))
{
$sql = "INSERT INTO author_percentage (author, percentage, start, end) VALUES ('" . $array['author'.$i] . "', '" . $array['percentage'.$i] . "', '" . $array['start'.$i] . "', '" . $array['end'.$i] . "')";
//execute SQL here, like$mysqli::query($sql);
$i++;
}
假设您的数组保存在变量$array中
有一个更简单的方法来做你想做的。将每个索引类别分组到单独的数组是非常优雅的。//存储子数组的父数组$data = array();
//ITERATE YOUR DATA INTO INDIVIDUAL ARRAYS INSIDE PARENT $data ARRAY
while($datasource != 0){
$data[] = array(
author => 31
percent => 10
start => 2014-05-20
end => 2014-05-21
);
现在您将拥有一个漂亮的数组树,您可以使用foreach快速操作它$data[0] = array(作者=> 31百分比=> 10Start => 2014-05-20结束=> 2014-05-21
);$data[1] = array(
author => 31
percent => 10
start => 2014-05-20
end => 2014-05-21
);
... and so on
快乐编码我的朋友。
从我的博客中破解一些PHP