更改新数组中数组内的变量值

时间:2022-12-12 20:18:39

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

如何,并且可以为数组中的变量添加不同的值,但是为了简化它可能与另一个数组一起执行;我需要在我的页面上存储进程及其cpu负载,但由于进程名称“丑陋”,我需要将其名称更改为正常的内容

so far i have managed to extract top 5 of them (mostly the same every time) like this:

到目前为止,我已经成功地提取了前5个(每次大致相同),如下所示:


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

我没有得到“process2”但更改名称(“第二个过程”)

Thanks in advance

提前致谢

1 个解决方案

#1


1  

You could do something like this, using the value from the first array as the key for the $new_values array:

你可以做这样的事情,使用第一个数组中的值作为$ new_values数组的键:

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:

编辑:我将在函数中包装它以检查$ new_values是否存在,否则回退到原始值:

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.

这样,如果传入$ array [1],它将返回$ new_values中的值(如果存在的话)(例如,在这种情况下为第二个进程),如果不存在(例如,如果传入$ array [5]并且它在$ new_values中没有漂亮的定义)它只会返回你传入的内容。

#1


1  

You could do something like this, using the value from the first array as the key for the $new_values array:

你可以做这样的事情,使用第一个数组中的值作为$ new_values数组的键:

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:

编辑:我将在函数中包装它以检查$ new_values是否存在,否则回退到原始值:

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.

这样,如果传入$ array [1],它将返回$ new_values中的值(如果存在的话)(例如,在这种情况下为第二个进程),如果不存在(例如,如果传入$ array [5]并且它在$ new_values中没有漂亮的定义)它只会返回你传入的内容。