3

Is it possible to compare two Array values ?

Say:

Array 1>         Array 2>
Values:          Values:
2                 11
36                13
65                11
78                1

Code sample:

If (Array1 >= Array2){

    echo"Not Available";
    }
    else
    {
    echo"Available";
    }

Expected Result:

Array ("Not Available",
       "Available",
       "Available",
       "Available")
4
  • Can you explain more? What do you mean by comparing here? Commented May 8, 2019 at 13:08
  • Compare the values of Array 1 [0] and Array 2 [0] and gives off a result of either Not available or Available and so on . Is it possible ? Commented May 8, 2019 at 13:10
  • Does order matter? Commented May 8, 2019 at 13:21
  • What do you mean by "available" or "not available"? How is this related to Laravel in any way? Commented May 24, 2019 at 14:05

5 Answers 5

2

You can use a callback function along with array_map function like this.

<?php 

function getResult($first, $second)
{
    return $first > $second ? 'Available' : 'Not Available';
}

$firstArray = [1,4,5,6];
$secondArray = [2,3,1,9];

$data = array_map('getResult', $firstArray, $secondArray);

echo '<pre>',print_r($data),'<pre>';

?>

You can customize the logic inside the function for more complex logic as well. You can add any number of arrays as per your requirement. I hope you understand.

Sign up to request clarification or add additional context in comments.

Comments

1

You can just do something simple like this:

function compareArrays(array $array1, array $array2): array
    {
        $itemCount = (count($array1) > count($array2)) ? $array1 : $array2;
        $returnArray = [];
        for($i = 0; $i < count($itemCount); $i++) {
            $returnArray[] = ($array1 >= $array2) ? 'Avalible' : 'Not Avalible';
        }
        return $returnArray;
    }

Main reason being is we don't know from your post if the arrays will always be the same size so you have to compare them to get the larger of the two then use that for the loop. After that it's just simple comparisons.

1 Comment

Your generation of $itemCount is a bit longwinded (IMHO) - you could just use $itemCount = max(count($array1), count($array2));
0

If both the arrays has same length, you can use for loop as below,

$temp = [];
for($i = 0; $i < count($array1);$i++){
    $temp[] = (($array1[$i] >= $array2[$i]) ? 'Not Available': 'Available');
}
print_r($temp);

Comments

0

Yes it is possible using a simple for loop as bellow:

for($i = 0; $i < count($array1); $i++){
  if($array1[$i] >= $array2[$i]){
    echo "Available"
  }else{
    echo "Not available";
  }
}

Note Here the two array must be of same size.

Comments

0

If array1 and array2 is always same size, then easy solution

$data = [];
foreach($array1 as $key => $value) {
    if (!isset($array2[$key])) { // for safety check for second array index bound
        break;
    }

    $data[] = $value >= $array2[$key] ? 'Not Available' : 'Available';
 }

return $data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.