Skip to content
Advertisement

Replace column values in result set using a lookup/dictionary array

You can find the entire code below.

I have 2 arrays. The first array looks like this:

// 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!',
);

The second array is this: (from a mysqli result set)

[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
    )

I want to replace the logMessage values in the second array with the corresponding $replacements value.

How it should look:

[logMessage] => **ID_CHAT_REQUEST_AMMO**

Must be

[logMessage] => Request Ammo!

Current code:

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;
    }
}

Advertisement

Answer

Maybe something like the following. Grabs the key values of $replacements and if the value of a key in $array is found, it will replace the the value with the $replacement value.

$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 ) 

EDIT

So let me apply the same logic to the rest of your array. Suppose we have the following array (I pulled this from your question above):

$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"

   ]
];

Now I can iterate over this array with the following:

$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
        }
    }
}

This will output the following:

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
        )

)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement