简体   繁体   中英

Extract data from php array

I have the following array in my Moodle plugin :

Array (
    [0] => stdClass Object (
        [id] => 32
        [sessionid] => 5
        [sessiontimezone] => CET
        [timestart] => 1464071400
        [timefinish] => 1464102000 ) 

    [1] => stdClass Object (
        [id] => 33
        [sessionid] => 5
        [sessiontimezone] => CET
        [timestart] => 1465281000
        [timefinish] => 1465311600 )
)

How to get the data . Right now, when I make:

$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));

I get only data from the frist array [0]

How to get the data from every array key and then the single values ? Thanks in advance.

The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.

If you have an array somewhere, then you can get fields from it by writing:

echo $myarray[0]->id;
echo $myarray[1]->id;
etc.

If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:

$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
    echo $session->id;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM