Reputation: 3207
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 check condition
if ($val === $value1 )
but it gives always true..
Means if $val = C and $value1 = A
ti's return true...
Please help me.
Thanks
Upvotes: 1
Views: 5312
Reputation: 1031
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;
}
}
}
?>
Upvotes: 4