Pravin Vavadiya Pravin Vavadiya - 10 days ago 5
PHP Question

How to compare two character in php?

Any one explain how can i compare two character in php

Here my Code:

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


Here I want to check
condition
if ($val === $value1 )
but it's givies always true..
Means if
$val = C and $value1 = A
ti's return true...
Please any one help me.

Thanks

Answer

Please try following code, actually you have to make inner foreach to outer and outer for loop to inner.

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

?>