2016-11-26 95 views
1

任何一個給我解釋一下我怎麼能在PHP中兩個字符比較如何比較兩個字符在PHP?

這裏我的代碼:

$unsorted = Array(
     "0" =>"0000C11", 
     "1" =>"0000A11", 
     "2" =>"0000C13", 
     "3" =>"0000D11", 
    ); 

$sortArr = array('A','B','C','D'); 

foreach ($unsorted as $key => $value) { 
     $val = substr($value,-3,1); 
     foreach ($sortArr as $key1 => $value1) { 
      if ($val === $value1) { 
       $sortArrFin[] = $value; 
      } 
     } 
} 
echo "<pre>"; 
print_r($sortArrFin); 

在這裏,我要檢查conditionif ($val === $value1)但它總是給真正.. 意味着如果$val = C and $value1 = A TI的迴歸真實... 請幫助我。

感謝

+0

你想做的和這裏一樣:http://stackoverflow.com/q/40816754/3933332? – Rizier123

+2

你的代碼適合我。 – arkascha

+0

我想檢查如何比較兩個字符....不排序數組.... –

回答

4

請嘗試下面的代碼,實際上你必須做出內部的foreach到外和外for循環內。

<?php 
    $unsorted = Array(
      "0" =>"0000C11", 
      "1" =>"0000E11", 
      "2" =>"0000C13", 
      "3" =>"0000D11", 
      "4" =>"0000A11" 
     ); 

    $sortArr = array('A','B','C','D','E'); 

    foreach ($sortArr as $key => $value) { 
     foreach ($unsorted as $key1 => $value1) {  
      $val = substr($value1,-3,1); 
      if ($val === $value) {    
       $sortArrFin[] = $value1;     
      } 
      } 
    } 

?>