我要做的是将具有相同类型结果的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循环以正确获得我需要的信息,请。我会非常感激,因为尽管我已经编码了一年多,但除了循环和我使用的数组从来都不是多维的之外,我几乎不需要处理任何循环。
$row = mysql_fetch_array($result)
包含两次数据库中的每个值,
(1( 作为一个数字数组并且
(2( 作为关联数组:
(1( $row[0]="Hello"
和
(2( $row['text']="Hello"
尝试... mysql_fetch_array($result,MYSQL_NUM)
仅获取数字索引,或者.. MYSQL_ASSOC)
以仅获得关联索引。
array_merge
"如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个。但是,如果数组包含数字键,则后一个数值将不会覆盖原始值,而是被追加。">
引用自http://www.php.net/manual/en/function.array-merge.php
我想这是在解释你得到的奇怪结果。
因此,首先设置mysql_get_array($result,MYSQL_NUM)
,然后将数组合并到$list
中,然后对它们进行排序(请参见php.net(,然后回显它们。
不过,$messages = array($list);
对我来说意义不大。
执行var_dump($list);
以了解如何构建此阵列。然后相应地调整循环。
顺便说一句,我忘了提到mysql_*不推荐使用,请改用mysqli或PDO。