Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将数组保存到数据库_Php_Mysql_Arrays - Fatal编程技术网

Php 将数组保存到数据库

Php 将数组保存到数据库,php,mysql,arrays,Php,Mysql,Arrays,我不熟悉PHP数组。。。但是我已经创建了一个数组来获取在表单中提交的两个日期之间的所有日期 见下文: $endDate=$_POST[todate]; $startDate =$_POST[fromdate]; print_r(getDatesFromRange( "$datef", "$dateto" )); function getDatesFromRange($startDate, $endDate) { $return = array($startDa

我不熟悉PHP数组。。。但是我已经创建了一个数组来获取在表单中提交的两个日期之间的所有日期

见下文:

$endDate=$_POST[todate];
            $startDate =$_POST[fromdate];

print_r(getDatesFromRange( "$datef", "$dateto" ));

function getDatesFromRange($startDate, $endDate)
{
    $return = array($startDate);
    $start = $startDate;
    $i=1;
    if (strtotime($startDate) < strtotime($endDate))
    {
       while (strtotime($start) < strtotime($endDate))
        {
            $start = date('Y-m-d', strtotime($startDate.'+'.$i.' days'));
            $return[] = $start;
            $i++;
        }
    }

    return $return;
}
$endDate=$\u POST[todate];
$startDate=$\发布日期[起始日期];
打印(getDatesFromRange(“$datef”,“$dateto”);
函数getDatesFromRange($startDate,$endDate)
{
$return=数组($startDate);
$start=$startDate;
$i=1;
if(strotime($startDate)
这将导致以下结果

数组([0]=>2016-10-10[1]=>2016-10-11[2]=>2016-10-12[3]=> 2016-10-13 [4] => 2016-10-14 [5] => 2016-10-15 [6] => 2016-10-16 [7] =>2016-10-17[8]=>2016-10-18[9]=>2016-10-19)


有没有办法使用PHP将这些日期保存到MySQL数据库中?

您可以使用/

示例-

$array = array("my", "litte", "array", 2);

$serialized_array = serialize($array); 
$unserialized_array = unserialize($serialized_array); 

var_dump($serialized_array); // gives back a string, perfectly for db saving!
var_dump($unserialized_array); // gives back the array again
您还可以使用内爆爆炸

范例-

<?php
    $vegetables[0] = "corn";
    $vegetables[1] = "broccoli";
    $vegetables[2] = "zucchini";
    $text = implode(",", $vegetables);
    echo $text;
?>


<?php
    $text = "corn, broccoli, zucchini";
    $vegetables = explode(", ", $text);
    print_r($vegetables);
?>

从范围do循环中获取日期后,插入数组的每个值

$date = getDatesFromRange( "$datef", "$dateto" );
    foreach ($date as $d){
        $sql = "INSERT INTO MyDatabase (dateCol)
                VALUES ('$d')";
        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }   
$date=getDatesFromRange(“$datef”,“$dateto”);
foreach(日期为$d){
$sql=“插入MyDatabase(dateCol)
值(“$d”)”;
if($conn->query($sql)==TRUE){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error; } }
您可以使用json\u encode/json\u decode在mysql中保存数组。它很容易使用

类似以下代码:

<?php
    $dates = array(
         '2016-10-10', '2016-10-11'
    );
    $json_dates = json_encode($dates);
    $arr = json_decode($json_dates, true);
?>

您不能在数据库中存储数组,但另一种解决方案是将数组转换为JSON字符串

您可以将数组转换为json,并将其存储到数据库中

$dates_in_json = json_encode($dates);
//Insert query
$insert = mysql_query("INSERT INTO table('dates') VALUES('".$dates_in_json ."')");
您可以在任何地方获取这些数据,并像这样转换为数组

$dates_array = json_decode($dates_in_json, TRUE);
这里第二个参数TRUE用于转换为数组,若不提供TRUE,json将转换为PHP对象