您可以在下面找到完整的代码。
我有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!',
);
第二个数组是:( Allready由php提取)
[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
)
我想用$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;
}
}
答案 0 :(得分:1)
也许这有帮助。它覆盖日志数组,检查logMessage元素的值并替换它。
<?php
foreach($logs as $log){
$replacementKey = str_replace('*', '', $log['logMessage']);
if(array_key_exists($replacementKey, $replacements)){
$log['logMessage'] = $replacements[$replacementKey];
}
}
?>
答案 1 :(得分:1)
可能类似以下内容。获取$replacements
的关键值,如果找到$array
中的键值,则会将该值替换为$replacement
值。
$array = [
"ID" => "2179",
"logDate" => "2016-05-12 22:10:38",
"ServerID" => "2",
"logSubset" => "Team",
"logSoldierName" => "OPTIMUS-GOLD",
"logMessage" => "ID_CHAT_REQUEST_AMMO",
"logPlayerID" => "1071"
];
$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!',
);
$keys = array_keys($replacements);
foreach($array as $k => $v) {
if(in_array($v, $keys)) {
$array[$k] = $replacements[$v];
}
}
print_r($array);
//PRINTS: Array ( [ID] => 2179 [logDate] => 2016-05-12 22:10:38 [ServerID] => 2 [logSubset] => Team [logSoldierName] => OPTIMUS-GOLD [logMessage] => Request Ammo! [logPlayerID] => 1071 )
因此,让我将相同的逻辑应用于数组的其余部分。假设我们有以下数组(我从上面的问题中提取了这个数据):
$array = [
"31" =>
[
"ID" => "2179",
"logDate" => "2016-05-12 22:10:38",
"ServerID" => "2",
"logSubset" => "Team",
"logSoldierName" => "OPTIMUS-GOLD",
"logMessage" => "ID_CHAT_REQUEST_AMMO",
"logPlayerID" => "1071"
],
"32" =>
[
"ID" => "2178",
"logDate" => "2016-05-12 22:10:34",
"ServerID" => "2",
"logSubset" => "Team",
"logSoldierName" => "CaptaineGamingFR",
"logMessage" => "ID_CHAT_REQUEST_RIDE",
"logPlayerID" => "1531"
],
"33" =>
[
"ID" => "2177",
"logDate" => "2016-05-12 22:10:27",
"ServerID" => "2",
"logSubset" => "Team",
"logSoldierName" => "CaptaineGamingFR",
"logMessage" => "ID_CHAT_GOGOGO",
"logPlayerID" => "1531"
],
"34" => [
"ID" => "2179",
"logDate" => "2016-05-12 22:10:38",
"ServerID" => "2",
"logSubset" => "Team",
"logSoldierName" => "OPTIMUS-GOLD",
"logMessage" => "ID_CHAT_REQUEST_AMMO",
"logPlayerID" => "1071"
]
];
现在我可以使用以下内容迭代这个数组:
$keys = array_keys($replacements); // Grab The Array Keys
foreach($array as $subarray => $a) { // For Each loop to pull out the subarrays
foreach($a as $k => $v) { // another to loop through those subarrays
if(in_array($v, $keys)) { // if the value is found as a key in the replacements array do the following
$array[$subarray][$k] = $replacements[$v]; // replace the current subarray index value with the value in the $replacements array
}
}
}
这将输出以下内容:
Array
(
[31] => Array
(
[ID] => 2179
[logDate] => 2016-05-12 22:10:38
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => OPTIMUS-GOLD
[logMessage] => Request Ammo!
[logPlayerID] => 1071
)
[32] => Array
(
[ID] => 2178
[logDate] => 2016-05-12 22:10:34
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => CaptaineGamingFR
[logMessage] => Request Ride!
[logPlayerID] => 1531
)
[33] => Array
(
[ID] => 2177
[logDate] => 2016-05-12 22:10:27
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => CaptaineGamingFR
[logMessage] => Go Go Go!
[logPlayerID] => 1531
)
[34] => Array
(
[ID] => 2179
[logDate] => 2016-05-12 22:10:38
[ServerID] => 2
[logSubset] => Team
[logSoldierName] => OPTIMUS-GOLD
[logMessage] => Request Ammo!
[logPlayerID] => 1071
)
)
答案 2 :(得分:1)
如果您在函数中硬编码 $replacements
,那么您不妨直接将其构建到您的 sql 中。使用 CASE
语句。
让 fetch_all()
返回一个 json 编码的数组(即使它是空的)。如果可能,最好在返回值中保持一致的数据类型。
public function serverChatlog(): string
{
$sql = "SELECT ID,
logDate,
ServerID,
logSubset,
logSoldierName,
CASE logMessage
WHEN 'ID_CHAT_ATTACK/DEFEND' THEN 'Attack/Defend!'
WHEN 'ID_CHAT_REQUEST_MEDIC' THEN 'Request Medic!'
WHEN 'ID_CHAT_REQUEST_AMMO' THEN 'Request Ammo!'
WHEN 'ID_CHAT_REQUEST_RIDE' THEN 'Request Ride!'
WHEN 'ID_CHAT_NEGATIVE' THEN 'Negative!'
WHEN 'ID_CHAT_SORRY' THEN 'Sorry!'
WHEN 'ID_CHAT_GOGOGO' THEN 'Go Go Go!'
WHEN 'ID_CHAT_AFFIRMATIVE' THEN 'Affirmative!'
WHEN 'ID_CHAT_REQUEST_ORDER' THEN 'Request Order!'
WHEN 'ID_CHAT_THANKS' THEN 'Thanks!'
ELSE logMessage
END logMessage,
logPlayerID
FROM `tbl_chatlog`
ORDER BY `ID` DESC";
return json_encode($this->connectDB()->query($sql)->fetch_all(MYSQLI_ASSOC), true);
}
看看你的方法变得多么干净、专业、直接和可读。
答案 3 :(得分:0)
我希望这有帮助。搜索数组键,如果存在键,则只需替换它。
foreach ($secondArray as &$value) {
$key = array_search ( trim($value['logMessage'], '*') , $value );
if (isset($key) && !empty($key)) {
$value['logMessage'] = $replacements[$key]
}
}