我遇到了将数组作为字符串回显的问题,我之前的代码如下所示:
$years = array('2013', '2014');
foreach($years as $year) {
//do rest of the code
我认为现在是让其余代码更加动态和“放手”的时候了,所以我准备了一个从数据库年份中选择的小函数,而不是在新年到来时手动输入它。
这是年份提取的代码:
function order_count_for_year($us_id) {
$orders_for_year = array();
$query = "SELECT DISTINCT YEAR(`date_posted`) FROM `orders` WHERE `us_id` = {$us_id}";
$query = mysql_query($query);
while($row = mysql_fetch_assoc($query)) {
$orders_for_year[] = $row;
}
return $orders_for_year;
}
因此,如果我为这个函数执行 foreach,它会正确返回 2013 和 2014,但我不能这样,所以我这样做了:
echo implode(', ', order_count_for_year($us_id));
它不起作用,它返回:array, array
,我不明白为什么它不起作用,它适用于应用程序中的不同数组,但不适用于这个......
最后我想出了这样的废话:
$some_variable = '';
foreach(order_count_for_year($us_id) as $test) { $some_variable .= $test['YEAR(`date_posted`)'] . ', '; }
echo $some_variable;
It works, but i didnt want to make it that complicated, i dont feel that this is good idea. Anybody has idea why isnt it working with implode, or how to make it work without loop?
Please dont post with answers that i have to use variables for implode, its not needed and even if - i tested it, doesnt work.