使用php

时间:2015-12-03 20:32:50

标签: php arrays loops

我有这样的代码

$events = array(


array(
    "id" => 101,
    "start_date" => "2016-04-05 08:00:00",
    "end_date" => "2016-04-09 09:00:00",
    "text" => "text1",
    "rec_type" => "week_2___3,5",
    "event_pid" => null,
    "event_length" => 3600
),

array(
    "id" => 102,
    "start_date" => "2016-04-06 12:00:00",
    "end_date" => "2016-04-06 18:00:00",
    "text" => "text2",
    "rec_type" => "",
    "event_pid" => null,
    "event_length" => null
),

array(
    "id" => 103,
    "start_date" => "2016-04-07 12:00:00",
    "end_date" => "2016-04-07 18:00:00",
    "text" => "text3",
    "rec_type" => "",
    "event_pid" => null,
    "event_length" => null
),

array(
    "id" => 104,
    "start_date" => "2016-04-08 12:00:00",
    "end_date" => "2016-04-08 18:00:00",
    "text" => "text4",
    "rec_type" => "",
    "event_pid" => null,
    "event_length" => null
)
);

现在我应该从MySQL获取一些数据并动态生成,但保持变量$events与此相同。

我在某种程度上尝试过while和for,但也许我在语法上犯了一些错误。

5 个答案:

答案 0 :(得分:2)

试试这个:

$events = //your array 
$sth = $dbh->query('SELECT * FROM tbl');

while ($row = $sth->fetchRow()) {
   array_push($events,$row)
}

答案 1 :(得分:2)

循环遍历数组中的数组?

foreach($arr1 as $arr2) {
    foreach($arr2 as $val) {
        // Your code
    }
}

答案 2 :(得分:0)

感谢您的回答

我会尝试更具体地按照某人的要求(我不是英语母语,如果我不能很好地解释我的问题,请原谅我),与此同时我会尝试一些答案,

$ event必须在类中发送,代码以这种方式继续

$events = array(


array(
    "id" => 101,
    "start_date" => "2016-04-05 08:00:00",
    "end_date" => "2016-04-09 09:00:00",
    "text" => "text1",
    "rec_type" => "week_2___3,5",
    "event_pid" => null,
    "event_length" => 3600
)


);


$title = "some_title";
require_once("../../ical/codebase/class.php");
$export = new ICalExporter();
$export->setTitle($title);
$ical = $export->toICal($events);

并且我在数据库中有一个表格,它以相同的方式组成,具有相同的字段,每个日历都有更多的事件,但是我需要一个可以被类接受的$事件,就像那样生成每次自动,这只是一个静态代码,我的工作示例。 我认为找到正确的语法只是一个问题,但它对我来说非常复杂我不知道为什么,我通常可以突然找到正确的解决方案)...无论如何都要检查以后是否还有其他答案

答案 3 :(得分:0)

我认为它必须是这样的,但它还没有工作......

$q_events = dbquery("select from * calendars where id_event = 1");


$events = array();
$i=0;

while ($q_ev=mysql_fetch_array($q_events)) {
            $events[$i]['id'] = $q_ev['event_id'];
            $events[$i]['start_date'] = $q_ev['start_date'];
            $events[$i]['end_date'] = $q_ev['end_date'];
            $events[$i]['text'] = $q_ev['text'];
            $events[$i]['rec_type'] = $q_ev['rec_type'];
            $events[$i]['event_pid'] = $q_ev['event_pid'];
            $events[$i]['event_length'] = $q_ev['event_length'];            
    $i++;   
        }


$title = $_GET['idstruttura']."_".changeLetters($_GET['nomestruttura']);
require_once("../../ical/codebase/class.php");
$export = new ICalExporter();
$export->setTitle($title);
$ical = $export->toICal($events);

如果我在查询后对数组执行print_r,结果就像这样

Array ( [0] => Array ( [id] => 413 [start_date] => 2016-02-15 00:00:00 [end_date] => 2016-02-21 00:00:00 [text] => fafafa [rec_type] => week_2___3,5 [event_pid] => 213 [event_length] => 518400 ) [1] => Array ( [id] => 408 [start_date] => 2016-01-11 00:00:00 [end_date] => 2016-01-18 00:00:00 [text] => evento_da_pannello [rec_type] => week_2___3,5 [event_pid] => 213 [event_length] => 604800 ) [2] => Array ( [id] => 410 [start_date] => 2015-12-28 00:00:00 [end_date] => 2015-12-31 00:00:00 [text] => aaa [rec_type] => week_2___3,5 [event_pid] => 213 [event_length] => 259200 ) )

看起来很正确,很奇怪

答案 4 :(得分:0)

我已经再次尝试了上一篇文章中的代码,只是做了一些小改动而不是关于数组的部分,幸运的是它可以工作

感谢大家的支持,对于这个精彩的网站,我已经关注了多年,即使我只是一个读者)