我正在尝试做的是采用具有相同类型结果的 2 个(当前,但最终将是 4 个)数组,将它们放在一起,按它们的值之一(post_time)对它们进行排序,并将它们回显到按该排序顺序显示屏幕。
我可以通过 while 循环通过它们各自的结果/数组来回显这些结果,但它们不会按总的发布时间正确排序,只能在它们各自的数组中。
我对嵌套的 foreach 循环感到困惑。我已经在我拥有的两个数组上尝试了 array_merge,然后将该数组放入另一个数组中,只是为了有一个数字键可以循环,但我被卡住了,感到困惑和疲倦。我已经完成了研究,但我无法将我看到的答案与我自己的特定代码进行比较。
我需要帮助。这就是我所拥有的。这是从数据库中检索单独的信息集/数组的代码。
include 'db_connect.php';
include 'core.php';
$get_char = mysql_query("SELECT character_id FROM characters WHERE account_id = '".$_SESSION['user_id']."'");
$char = mysql_result($get_char,0,'character_id');
$get_location = mysql_query("SELECT current_location, x_cord, y_cord, inside_building FROM characters WHERE character_id = $char");
$location = mysql_result($get_location, 0, 'current_location');
$x = mysql_result($get_location,0, 'x_cord');
$y = mysql_result($get_location,0,'y_cord');
$inside = mysql_result($get_location,0,'inside_building');
//get all messages where current characters arrival time is less than post_time
//AND arrival location is the same as posting location
$get_arrival = mysql_query("SELECT * FROM arrive_exit WHERE character_id = $char ORDER BY arrival DESC");
while($arrivals = mysql_fetch_array($get_arrival)){
$arrival = $arrivals['arrival'];
$arrival_location = $arrivals['location'];
$unix_arrival = strtotime($arrival);
$exit = $arrivals['exit'];
$unix_exit = strtotime($exit);
if(!isset($exit)){
$get_logs = mysql_query("SELECT character_id, log, post_time, event FROM character_logs WHERE UNIX_TIMESTAMP(post_time) >= $unix_arrival
AND location = $arrival_location ORDER BY post_time DESC");
}else{
$get_logs = mysql_query("SELECT character_id, log, post_time, event FROM character_logs WHERE UNIX_TIMESTAMP(post_time) >= $unix_arrival
AND location = $arrival_location AND UNIX_TIMESTAMP(post_time) <= $unix_exit ORDER BY post_time DESC");
}
$o_logs = mysql_fetch_array($get_logs);
//CHECK FOR LOGS IN BUILDING LOGS
if(!isset($exit)){
$check_logs = mysql_query("SELECT * FROM building_logs WHERE UNIX_TIMESTAMP(post_time) >= $unix_arrival
AND current_location = $arrival_location");
}else{
$check_logs = mysql_query("SELECT * FROM building_logs WHERE UNIX_TIMESTAMP(post_time) >= $unix_arrival
AND current_location = $arrival_location");
}
$building_check = mysql_num_rows($check_logs);
if($building_check >= 1){
$get_room = mysql_query("SELECT room_id, building_current_id, building_id FROM building_logs WHERE character_id = $char");
$room_id = mysql_result($get_room,0,'room_id');
$building_current = mysql_result($get_room,0,'building_current_id');
$building_id = mysql_result($get_room,0,'building_id');
if(!isset($exit)){
$get_logs = mysql_query("SELECT character_id, log, post_time, event FROM building_logs WHERE UNIX_TIMESTAMP(post_time) >= $unix_arrival
AND current_location = $arrival_location AND x = $x AND y = $y AND room_id = $room_id ORDER BY post_time DESC");
}else{
$get_logs = mysql_query("SELECT character_id, log, post_time, event FROM building_logs WHERE UNIX_TIMESTAMP(post_time) >= $unix_arrival
AND current_location = $arrival_location AND x = $x AND y = $y AND room_id = $room_id
AND UNIX_TIMESTAMP(post_time) <= $unix_exit ORDER BY post_time DESC");
}
$b_logs = mysql_fetch_array($get_logs);
}
}
这是我试图用来操纵检索结果的非工作代码。
$list = array_merge((array)$o_logs,(array)$b_logs);
$messages = array($list);
//echo out info here.
foreach($messages as $array){
foreach($array as $log_key => $value){
//log_key in this case could be character_id, event, post_time, so on which is not preferable
echo $log_key.' : '.$value.'<br>';
}
}
哪个输出:
0 : 62
character_id : 64
1 : This is a testing log from a different person at an earlier time in a DIFFERENT location.
log : This is a log from inside a building now.
2 : 2013-02-22 04:04:00
post_time : 2013-02-26 19:00:00
3 : 0
event : 0
4 : 64
5 : This is a log from inside a building now.
6 : 2013-02-26 19:00:00
7 : 0
所以,显然我得到的键也是来自内部 foreach 循环的字符串。它们来自第二个“building_logs”查询。我没想到我从上面快速记下的循环嵌套中得到的输出正是我想要的,但我会说我不知道为什么会这样。
我需要帮助了解如何嵌套 foreach 循环以正确获取我需要的信息,拜托。我将非常感激,因为即使我已经编写了一年多的代码,除了 while 循环之外,我几乎不需要处理任何循环,而且我使用的数组从来都不是多维的。