【发布时间】:2016-09-13 15:11:14
【问题描述】:
您可以在下面找到整个代码。
我有 2 个数组。第一个数组如下所示:
// Replace ID_CHAT_ with readable value
$replacements = array(
'ID_CHAT_ATTACK/DEFEND' => 'Attack/Defend!',
'ID_CHAT_REQUEST_MEDIC' => 'Request Medic!',
'ID_CHAT_REQUEST_AMMO' => 'Request Ammo!',
'ID_CHAT_REQUEST_RIDE' => 'Request Ride!',
'ID_CHAT_NEGATIVE' => 'Negative!',
'ID_CHAT_SORRY' => 'Sorry!',
'ID_CHAT_GOGOGO' => 'Go Go Go!',
'ID_CHAT_AFFIRMATIVE' => 'Affirmative!',
'ID_CHAT_REQUEST_ORDER' => 'Request Order!',
'ID_CHAT_THANKS' => 'Thanks!',
);
第二个数组是这样的:(来自 mysqli 结果集)
[31] => Array
(
[ID] => 2179
[logDate] => 2016-05-12 22:10:38
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => OPTIMUS-GOLD
[logMessage] => ID_CHAT_REQUEST_AMMO
[logPlayerID] => 1071
)
[32] => Array
(
[ID] => 2178
[logDate] => 2016-05-12 22:10:34
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => CaptaineGamingFR
[logMessage] => ID_CHAT_REQUEST_RIDE
[logPlayerID] => 1531
)
[33] => Array
(
[ID] => 2177
[logDate] => 2016-05-12 22:10:27
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => CaptaineGamingFR
[logMessage] => ID_CHAT_GOGOGO
[logPlayerID] => 1531
)
[34] => Array
(
[ID] => 2176
[logDate] => 2016-05-12 22:10:11
[ServerID] => 2
[logSubset] => Global
[logSoldierName] => CaptaineGamingFR
[logMessage] => cool des francais
[logPlayerID] => 1531
)
[35] => Array
(
[ID] => 2175
[logDate] => 2016-05-12 22:10:08
[ServerID] => 2
[logSubset] => Global
[logSoldierName] => Minotaures
[logMessage] => et sa s affiche sur tt les serveur
[logPlayerID] => 1337
)
我想将第二个数组中的 logMessage 值替换为对应的$replacements 值。
它应该是什么样子:
[logMessage] => **ID_CHAT_REQUEST_AMMO**
必须
[logMessage] => Request Ammo!
当前代码:
public function serverChatlog( $serverID = null ) {
// Replace ID_CHAT_ with readable value
$replacements = array(
'ID_CHAT_ATTACK/DEFEND' => 'Attack/Defend!',
'ID_CHAT_REQUEST_MEDIC' => 'Request Medic!',
'ID_CHAT_REQUEST_AMMO' => 'Request Ammo!',
'ID_CHAT_REQUEST_RIDE' => 'Request Ride!',
'ID_CHAT_NEGATIVE' => 'Negative!',
'ID_CHAT_SORRY' => 'Sorry!',
'ID_CHAT_GOGOGO' => 'Go Go Go!',
'ID_CHAT_AFFIRMATIVE' => 'Affirmative!',
'ID_CHAT_REQUEST_ORDER' => 'Request Order!',
'ID_CHAT_THANKS' => 'Thanks!',
);
// Create an array.
$output = array();
// Query the database
$query = $this->connectDB()->query( 'SELECT * FROM `tbl_chatlog` ORDER BY `ID` DESC' );
$result = $query->num_rows;
$array = $query->fetch_all(MYSQLI_ASSOC);
// Return the results in JSON format
if( $result > 0 ) {
$output = json_encode( $array, JSON_PRETTY_PRINT );
return json_decode( $output, true );
} else {
return false;
}
}
【问题讨论】:
-
您已经尝试过什么了吗?如果您需要线索,请查看array_search 方法
-
是的,我尝试了很多东西。还有 Allen Butler 的遮阳篷。但是,它只替换 1 个值而不是所有值。
标签: php mysql arrays mysqli replace