How To Compare Array Elements With Each Other In PHP

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

compare array elements with each others using for loop or for each loop in php and you check each elements

compare all array elements in PHP

let's learn

1.Create Array.

An array is a  data structure that stores one or more similar type of values in a single variable.

$array=["Raspberries", "Strawberries", "Blueberries", "Gooseberries","Farkleberry"];


2.Creating loops to compare array all elements each others.
1.for loop.

  • After creating array elements
  • Count array elements using count function.
  • The for loop by $i variable that loop goes 0 to n-2.

  • Create inner for loop using $j variable  the inner loop takes the value from the outer for loop value as initial and goes to the end.
     
$count = count($array);
for ($i = 0; $i < $count - 1; $i++) {//outerloop which goes 0 to n-2
    for ($j = $i + 1; $j < $count; $j++) {// inner loop initial the outerloop values and goes until end
        // compare $array[$i] with $array[$j]...
    }
}

2.foreach loop.

  • after creating array elements.
  • use array_values it will return all the values of an array
  • Create foreach loop by $x variable that loop  through a block of code for each element in an array.
  • Create inner foreach loop that initial the value of outer foreach loop.
 
 foreach($x as $key1 => $item){
    foreach($x as $key2 => $item2){
               //compare 
     }
  }


Complete code for compare all array elements with each other.
 

<!DOCTYPE html>
<html>
<head>
    <title>Compare all array elements with each other in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Compare all array elements with each other in PHP</h1>
    </div>
    <h3>Using for loop</h3>
    <div class="well">
        <?php
        $array_1=["Raspberries", "Strawberries", "Blueberries", "Gooseberries","Farkleberry"];
        $count = count($array_1);//count function to count array elements
        for ($i = 0; $i < $count - 1; $i++) {
            for ($j = $i + 1; $j < $count; $j++) {
                echo $array_1[$i] . "  vs  " . $array_1[$j] . "<br>";
            }
        }
        ?>
    </div>
    <h3>Using foreach loop</h3>
    <div class="well">
        <?php
        $array_2 = array('R' => "Raspberries", 'S' => "Strawberries", 'B' => "Blueberries", 'G' => "Gooseberries", 'F' => "Farkleberry");
//        $array_2=["Raspberries", "Strawberries", "Blueberries", "Gooseberries","Farkleberry"];// works also with this array
        $x = array_values($array_2);
        $y = array_keys($array_2); // Optional
        foreach($x as $key1 => $item){
            foreach($x as $key2 => $item2){
                if($key2 <= $key1){

                    continue;
                }
                if($item == $item2){

                    continue;
                }
//        echo $item . ' vs ' . $item2 . '<br>';
                echo $y[$key1] . ' vs ' . $y[$key2] . '<br>'; // Optional
            }
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post