【发布时间】:2014-01-11 08:02:17
【问题描述】:
我正在尝试组织一组通过数据库查询获得的结果。这是我用来执行此操作的代码:
$time_slots = $wpdb->get_results($query);
print_r($time_slots);
echo ("<br/><br/><br/><br/>");
/*organize slots into array*/
$openings = array();
foreach($time_slots as $ts)
{
if(empty($openings))
{
echo("Empty Array: ");
echo ("<br/>");
echo ("<br/>");
echo("Inserting: ");
print_r($ts);
echo ("<br/>");
echo ("<br/>");
$openings[$ts->route_date] = $ts;
echo("contents of Opening: ");
echo ("<br/>");
echo ("<br/>");
print_r($openings);
echo ("<br/><br/><br/><br/>");
}
elseif (array_key_exists($ts->route_date, $openings))
{
echo("Same Day");
echo ("<br/>");
echo ("<br/>");
echo("Inserting: ");
print_r($ts);
echo ("<br/>");
echo ("<br/>");
array_push($openings[$ts->route_date][$ts->name], $ts);
echo("contents of Opening: ");
echo ("<br/>");
echo ("<br/>");
print_r($openings);
echo ("<br/><br/><br/><br/>");
}
else
{
echo("New Day : ");
echo ("<br/>");
echo ("<br/>");
echo("Inserting: ");
print_r($ts);
echo ("<br/>");
echo ("<br/>");
$openings[$ts->route_date] = $ts;
echo("contents of Opening: ");
echo ("<br/>");
echo ("<br/>");
print_r($openings);
echo ("<br/><br/><br/><br/>");
}
}
/*return results*/
$result['openings'] = $openings;
$result['time'] = $time_slots;
$result['begin'] = $begin;
$result['end'] = $end;
$result['query'] = $query;
$result['type'] = "success";
$result = json_encode($result);
print_r($openings);
这是我print_r$ts 时单个结果的样子:
stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 )
这是一个循环的样子。你会注意到逻辑是有效的,并且一切都在它应该去的地方,但是添加 t:
> Empty Array:
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 1 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 2 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 3 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-10 [name] => 4 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) )
New Day :
Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) )
Same Day
Inserting: stdClass Object ( [route_date] => 2014-01-11 [name] => 1 [openings] => 1 [appointments] => 0 )
contents of Opening:
Array ( [2014-01-10] => stdClass Object ( [route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-11] => stdClass Object ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [appointments] => 0 ) )
当一切都说完了,我得到了这个结果:
Array ([2014-01-10] => stdClass Object ([route_date] => 2014-01-10 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01 -11] => stdClass 对象 ( [route_date] => 2014-01-11 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-01-12] => stdClass 对象 ( [ route_date] => 2014-01-12 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-13] => stdClass Object ([route_date] => 2014-01-13 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-14] => stdClass Object ([route_date] => 2014-01-14 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-01-15] => stdClass 对象 ( [route_date] => 2014-01-15 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-01-16] => stdClass Object ([route_date] => 2014-01-16 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-17] = > stdClass Object ([route_date] => 2014-01-17 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-18] => stdClass Object ([route_date] => 2014-01-18 [姓名] => 0 [职位] => 1 [约会s] => 0 ) [2014-01-19] => stdClass Object ( [route_date] => 2014-01-19 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014- 01-20] => stdClass 对象 ( [route_date] => 2014-01-20 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-01-21] => stdClass 对象 ( [route_date] => 2014-01-21 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-22] => stdClass Object ([route_date] => 2014-01- 22 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-23] => stdClass Object ([route_date] => 2014-01-23 [name] => 0 [openings ] => 1 [约会] => 0) [2014-01-24] => stdClass 对象 ([route_date] => 2014-01-24 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-01-25] => stdClass Object ( [route_date] => 2014-01-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-26] => stdClass Object ( [route_date] => 2014-01-26 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-27] => stdClass Object ( [route_date] = > 2014-01-27 [姓名] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-01-28] => stdClass Object ([route_date] => 2014-01-28 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01 -29] => stdClass Object ( [route_date] => 2014-01-29 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-01-30] => stdClass Object ( [ route_date] => 2014-01-30 [name] => 0 [openings] => 1 [appointments] => 0) [2014-01-31] => stdClass Object ([route_date] => 2014-01-31 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-02-01] => stdClass 对象 ([route_date] => 2014-02-01 [名称] => 0 [职位] => 1 [约会] => 0 ) [2014-02-02] => stdClass 对象 ( [route_date] => 2014-02-02 [名称] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-02-03] => stdClass Object ([route_date] => 2014-02-03 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-04] = > stdClass Object ([route_date] => 2014-02-04 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-05] => stdClass Object ( [route_date] => 2014-02-05 [姓名] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-02-06] => stdClass 对象 ( [route_date] => 2014-02-06 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02- 07] => stdClass Object ( [route_date] => 2014-02-07 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-08] => stdClass Object ( [route_date ] => 2014-02-08 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-09] => stdClass Object ([route_date] => 2014-02-09 [ name] => 0 [openings] => 1 [appointments] => 0) [2014-02-10] => stdClass Object ([route_date] => 2014-02-10 [name] => 0 [openings] = > 1 [约会] => 0) [2014-02-11] => stdClass 对象 ([route_date] => 2014-02-11 [名称] => 0 [职位] => 1 [约会] => 0) [2014-02-12] => stdClass 对象([route_date] => 2014-02-12 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-13] => stdClass 对象([route_date] => 2014-02-13 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-14] => stdClass 对象([route_date] => 2014 -02-14 [姓名] => 0 [职位空缺] => 1 [约会] => 0 ) [2014-02-15] => stdClass Object ( [route_date] => 2014-02-15 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02- 16] => stdClass 对象 ( [route_date] => 2014-02-16 [name] => 0 [openings] => 1 [约会] => 0 ) [2014-02-17] => stdClass 对象 ( [route_date ] => 2014-02-17 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-18] => stdClass Object ([route_date] => 2014-02-18 [ name] => 0 [openings] => 1 [appointments] => 0) [2014-02-19] => stdClass Object ([route_date] => 2014-02-19 [name] => 0 [openings] = > 1 [约会] => 0) [2014-02-20] => stdClass 对象 ([route_date] => 2014-02-20 [名称] => 0 [职位] => 1 [约会] => 0) [2014-02-21] => stdClass 对象([route_date] => 2014-02-21 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-22] => stdClass 对象([route_date] => 2014-02-22 [name] => 0 [openings] => 1 [appointments] => 0)[2014-02-23] => stdClass 对象([route_date] => 2014 -02-23 [姓名] => 0 [职位空缺] => 1 [约会] = > 0 ) [2014-02-24] => stdClass Object ([route_date] => 2014-02-24 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-25 ] => stdClass Object ( [route_date] => 2014-02-25 [name] => 0 [openings] => 1 [appointments] => 0 ) [2014-02-26] => stdClass Object ( [route_date] => 2014-02-26 [name] => 0 [openings] => 1 [appointments] => 0) [2014-02-27] => stdClass Object ([route_date] => 2014-02-27 [name ] => 0 [职位空缺] => 1 [约会] => 0 ) )
您会注意到只有第一个对象的第一个实例被添加到新数组$openings。
更新:
我刚刚意识到我需要一个多维数组,但是当我尝试添加第二维时,我收到以下错误:
Cannot use object of type stdClass as array
我在这里做错了什么?我想要一个数组$openings[route_date][number],但我无法让它工作。任何帮助都会很棒。
【问题讨论】:
-
顺便说一句,请在输出前添加
echo "<pre>"并在其后添加echo "</pre>"来打印此类数组。 -
我不知道你能做到这一点!太棒了!!!!谢谢!!!!
-
不客气,只是您应该注意的提示.. :)
标签: php mysql multidimensional-array