[英]PHP, MYSQL combining arrays from mysqli_fetch_array
此代碼是飼料事件到功能的一部分FullCalendar並從稍微修改此 。 我的編碼知識充其量是最少的,並且已經從事了幾天。
解:
讓我們假設MYSQL表:
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;
讓我們假設函數:
{
$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);
}
問題:
$e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
$e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);
無論輸入什么數據,結果均為4。 添加的數組越多,即使數據是字符串,$ e的值也只會增加1。 對我來說,這是進步,因為我嘗試了$ a + $ b或array_combine等其他方法,設置了多個數組實例,例如:
$e['start'] = $fetch['sdate'];
$e['start'] = $fetch['stime'];
$e['end'] = $fetch['edate'];
$e['end'] = $fetch['etime'];
而且沒有一個工作。 甚至有可能合並數組以產生除其中包含的數組數量以外的其他內容? 非常感謝您抽出寶貴的時間和閱讀。
事實:
$e['start'] = array_push($e,$fetch['sdate'],$fetch['stime']);
$e['end'] = array_push($e,$fetch['edate'],$fetch['etime']);
如果考慮到array_push的定義(它聲明返回),則返回4是完全正常的,我引用:
數組中新的元素數。
由於所討論的數組是e
,我們知道已經有索引為start
, end
的元素,並且array_push包含另外兩個元素,其結果為4。
因此,要知道關於array_push的重要事情是它不返回數組,而是修改作為參數給出的第一個數組。 因此,為了使您的示例正常工作,您只需執行以下操作:
array_push($e['start'], $fetch['sdate'], $fetch['stime']);
array_push($e['end'], $fetch['edate'], $fetch['etime']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.