我想要做的是以相同类型的结果作为2(当前,但最终会有4个总数)数组,将它们放在一起,按其值之一对它们进行排序(post_time),并使用echo他们以该排序顺序进入屏幕。如何使用foreach循环来检索由两个数组组成的嵌套数组中的信息?
我可以通过它们各自的结果/数组使用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循环之外,我几乎不得不处理任何循环,并且与之一起工作的数组从不是多维的。
非常感谢,这非常有帮助。是的,对不起,我应该注意到我还没有迁移到mysqli,因为我在知道mysql被弃用之前开始了这个项目。稍后我会一举改变它。再次感谢你的帮助。 – Nyx 2013-03-03 01:52:39