2017-04-13 4 views
0

str_replace, str_replace 또는 다른 옵션을 사용하지 않는 것이 가장 좋습니다.str_replace, str_replace 또는 다른 옵션을 사용하지 않는 것이 가장 좋습니다.

예를 들어 XML 파일을 기반으로 PHP에서 생성 된 배열이 비어 있으며 변경이 필요한 변수가 있습니다. 값이 배열에 있고 변수에 표준 값이 있으면 값을 다른 값 (번역, 다른 단어 등)으로 변경해야합니다.

나는 아래의 코드로이 작업을 수행 할 수 있습니다

<?php 
$ethnic_later = 'niet opgegeven'; 
$ethnic_array = array( 'Blank', 
         'getint', 
         'Aziatisch', 
         'Zuid-Amerikaans' 
        ); 
if (in_array('Blank', $ethnic_array) && $ethnic == 'Blank'){ 
    $ethnic = str_replace($ethnic, 'blanke', $ethnic); 
}elseif (in_array('getint', $ethnic_array) && $ethnic == 'getint'){ 
    $ethnic = str_replace($ethnic, 'getinte', $ethnic); 
}elseif (in_array('Aziatisch', $ethnic_array) && $ethnic == 'Aziatisch'){ 
    $ethnic = str_replace($ethnic, 'Aziatische', $ethnic); 
}elseif(in_array($ethnic, $ethnic_array) && $ethnic == 'Zuid-Amerikaans'){ 
    $ethnic = str_replace($ethnic, 'Zuid Amerikaanse', $ethnic); 
}else{ 
    $ethnic = str_replace($ethnic, $ethnic_later, $ethnic); 
} 

echo 'een mooie ' . $ethnic . ' kleur'; 
?> 

아니면 그냥 아래와 같이 PHP 함수 str_replace()를 사용하지 않고 $ethnic의 값을 덮어 쓸 수 있습니다 : 더 가능성이있다

<?php 
$ethnic_later = 'niet opgegeven'; 
$ethnic_array = array( 'Blank', 
         'getint', 
         'Aziatisch', 
         'Zuid-Amerikaans' 
        ); 
if (in_array('Blank', $ethnic_array) && $ethnic == 'Blank'){ 
    $ethnic = 'blanke'; 
}elseif (in_array('getint', $ethnic_array) && $ethnic == 'getint'){ 
    $ethnic = 'getinte'; 
}elseif (in_array('Aziatisch', $ethnic_array) && $ethnic == 'Aziatisch'){ 
    $ethnic = 'Aziatische'; 
}elseif(in_array($ethnic, $ethnic_array) && $ethnic == 'Zuid-Amerikaans'){ 
    $ethnic = 'Zuid Amerikaanse'; 
}else{ 
    $ethnic = $ethnic_later; 
} 

echo 'een mooie ' . $ethnic . ' kleur'; 
?> 

이 일을 이런 식으로하면 위의 2 게시 ... str_replace() 사용하지 않고 마지막 옵션을 사용하여보다 빠른 것입니다 str_replace() ...

질문 : PHP을 사용하여 원하는 작업을 수행하는 가장 좋은 방법은 무엇이며 어떻게 이러한 종류의 작업을 수행합니까?

+0

'White', 'Black', 'Yellow'' ftfy – user2176127

+0

@ user2176127 예, 배열을 만드는 또 다른 방법은 읽기 쉽지 않지만'array'('White', 'Black', 'Yellow ');'빨리? – jagb

답변

0

당신이 민족 $를 기대 값으로 인덱싱 배열을 만들고 문자열을 포함 당신과 같이, 매핑하기 위해 $ 민족 변수를 싶습니다

<?php 
$ethnic_later = 'niet opgegeven'; 
$ethnic_array = array( 'Blank' => 'blanke', 
       'getint' => 'getinte', 
       'Aziatisch' => 'Aziatische', 
       'Zuid-Amerikaans' => 'Zuid Amerikaanse' 
       ); 
if (isset($ethnic_array[$ethnic])) { 
     $ethnic = $ethnic_array[$ethnic]; 
}else{ 
     $ethnic = $ethnic_later; 
} 

echo 'een mooie ' . $ethnic . ' kleur'; 

을 원할 때 더 많은 $ ethnics를 추가하고 배열에 요소를 더 추가하십시오.

+0

입력 해 주셔서 감사합니다. 코드 길이가 짧으며 속도가 빠릅니다. 속도는 테스트하지 않았습니다. – jagb