Home  >  Article  >  Backend Development  >  In php, traverse the array to find whether the array element exists and the current string. If it does not exist, output the current string. If it exists, output the key value of the array.

In php, traverse the array to find whether the array element exists and the current string. If it does not exist, output the current string. If it exists, output the key value of the array.

WBOY
WBOYOriginal
2016-08-23 09:17:491951browse

<code><?php
$lang_Log=array(
    'rep-cache during mirror is cleard'=>'规则缓冲清除',
    'Starting replication'=>'开始规则',
    'Wait -> Brk'=>'镜像状态改变,由等待到停止状态',
    'OK -> Wait'=>'镜像状态改变,由OK到等待状态',

);


$logs = sd_get_log_sys_by_uuid(uuid);

for($i=0; $i<$count(logs); $i++) {
    if($lang_Log[$logs[$i]]){
        echo $lang_Log[$logs[$i]];
    }else{
        foreach ($lang_Log as $k=> $v) {
            $pos = strpos($logs[$i], $k);
            if($pos !== false){
                echo $v;
                break;
            }
        } 
    }
}

?>
</code>

What I want to do is to traverse the array to find whether the array element exists in the current string. If it does not exist, output the current string. If it exists, output the key value of the array.
It’s the translation function.

1. Determine whether the log has corresponding Chinese characters in the array (because some logs are dead).
2. Determine whether part of the string in the log has corresponding Chinese characters (some logs contain statements containing variables, so partial strings need to be used Match and then translate).
3. Because there is a loop statement of logs array in the outer layer, so use break instead of exit.

Reply content:

<code><?php
$lang_Log=array(
    'rep-cache during mirror is cleard'=>'规则缓冲清除',
    'Starting replication'=>'开始规则',
    'Wait -> Brk'=>'镜像状态改变,由等待到停止状态',
    'OK -> Wait'=>'镜像状态改变,由OK到等待状态',

);


$logs = sd_get_log_sys_by_uuid(uuid);

for($i=0; $i<$count(logs); $i++) {
    if($lang_Log[$logs[$i]]){
        echo $lang_Log[$logs[$i]];
    }else{
        foreach ($lang_Log as $k=> $v) {
            $pos = strpos($logs[$i], $k);
            if($pos !== false){
                echo $v;
                break;
            }
        } 
    }
}

?>
</code>

What I want to do is to traverse the array to find whether the array element exists in the current string. If it does not exist, output the current string. If it exists, output the key value of the array.
It’s the translation function.

1. Determine whether the log has corresponding Chinese characters in the array (because some logs are dead).
2. Determine whether part of the string in the log has corresponding Chinese characters (some logs contain statements containing variables, so partial strings need to be used Match and then translate).
3. Because there is a loop statement of logs array in the outer layer, so use break instead of exit.

Why don’t you goelse?
You loop through each word. The first one in the loop is rep-cache during mirror is cleard. Does
exist in $s?
Doesn’t exist
Can we goelse?
Go
luck not runningexit;?
Run
Do you want to continue?
Don’t continue
Do you still need to check the following words?
No checking

<code>foreach ($lang_Log as $k=> $v) {
    $pos = strpos($s, $k);
    if($pos !== false){
        echo $v;
        break;
    }
} 
</code>

$s = "mirror return 0, peer 94, bytes recv [En/De] [2005667596/2005667596 = 100]";
foreach ($lang_Log as $k=> $v) {

<code>$pos = strpos($s, $k);
if($pos !== false){
    echo 'value = ' .  $v . "<br/>;
}else{
    echo 'key = ' .  $k . "<br/>;
}</code>

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn