如何在php中比较两个字符?

时间:2016-11-26 10:01:23

标签: 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);

在这里,我想查看condition if ($val === $value1 ),但它始终是真的.. 意味着如果$val = C and $value1 = A ti返回true ... 请帮帮我。

由于

1 个答案:

答案 0 :(得分:4)

请尝试以下代码,实际上你必须将内部foreach设置为外部和外部以循环到内部。

    <?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;                
            }
          }
    }

?>