简体   繁体   中英

PHP, MYSQL combining arrays from mysqli_fetch_array

This code is part of a function that feeds events into FullCalendar and is modified slightly from this . My coding knowledge is minimal at best and have been at this for a few days now.

  • My MYSQL data comes in two parts
  • Separate DATE and TIME
  • Current function only accepts DATETIME or TIMESTAMP format

Solution:

  • Combine DATE and TIME from arrays to form DATETIME

Let's assume the MYSQL table:

CREATE TABLE `test` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(11) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `sdate` date NOT NULL,
  `stime` varchar(5) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `edate` date NOT NULL,
  `etime` varchar(5) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Let's assume the function:

{
    $events = array();
    $query = mysqli_query($con, "SELECT * FROM test");
    while($fetch = mysqli_fetch_array($query,MYSQLI_ASSOC))
    {
    $e = array();
    $e['id'] = $fetch['id'];
    $e['title'] = $fetch['title'];
    $e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
    $e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);

    array_push($events, $e);
    }
    echo json_encode($events);
}

Problem:

$e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
$e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);

Results in '4' no matter what data is input. The more arrays added, the value of $e only increases by 1 even though the data is a string. To me this is progress because I've tried others like $a + $b or array_combine, setting multiple array instancess like:

$e['start'] = $fetch['sdate'];
$e['start'] = $fetch['stime'];
$e['end'] = $fetch['edate'];
$e['end'] = $fetch['etime'];

And none have worked. Is it even possible to combine the arrays to result in something other than the number of arrays contained within? Thank you all so much for the time and reading.

The fact that :

$e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
$e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);

Returns 4 is perfectly normal if you consider the definition of array_push which states that it returns, I quote :

the new number of elements in the array.

Since the array in question is e , we know there is already the element with index start , end and your array_push contains two other elements, which leads to 4.

So the important thing to know about array_push is that it does not return an array but modify the first array given as parameter. So for your example to work, you should simply do :

array_push($e['start'], $fetch['sdate'], $fetch['stime']);
array_push($e['end'], $fetch['edate'], $fetch['etime']);

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