Reputation: 123
How to, and is it possible to add different value to a variable inside an array, but to simplify it possibly to do it with another array; i need to store processes and their cpu load on my page, but since process names are "ugly", i need to change their name to something normal looking
so far i have managed to extract top 5 of them (mostly the same every time) like this:
$array[0] = "process1"; $array[1] = "process2"; $array[2] = "process3";
Now i want to add as many possibilities as possible to change some prettier values to them
$new_values = array(
"process1" == "Process name as i want it",
"process2" == "Second process"
);
So when i call say
$array[1]
i don't get "process2" but changed name ("Second process")
Thanks in advance
Upvotes: 0
Views: 178
Reputation: 24406
You could do something like this, using the value from the first array as the key for the $new_values
array:
echo $new_values[$array[1]]; // Second process
Edit: I'll wrap this inside a function to check for the $new_values
existence, otherwise fall back to the original value:
function displayPretty($key) {
global $new_values; // get the $new_values array from global scope
if(array_key_exists($key, $new_values))
return $new_values[$key]; // return pretty name if it exists
return $key; // return original value otherwise
}
echo displayPretty($array[1]);
This way, if you pass in $array[1]
it will return the value from $new_values
if it exists (e.g. Second process
in this case), and if it doesn't (e.g. if you passed in $array[5]
and that doesn't have a pretty definition in $new_values
) it will just return what you passed in.
Upvotes: 1