How to compare two character in php?

103

Any one explain me how can I compare two characters 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 checkconditionif ($val === $value1 ) but it gives always true.. Means if$val = C and $value1 = A ti's return true... Please help me.

Thanks

645

Answer

Solution:

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

?>

People are also looking for solutions to the problem: php - Adding a rewrite rule to wordpress using regex

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.