0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Side note: your syntax in the second array is incorrect, should be =>
Yep, figured that out, but now how can i call any $array[x] to display it's value, but if a value is inside new array to display the corrected value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.