How to compare all positions of an array in PHP

Asked

Viewed 5,645 times

0

I filled my two arrays that I need to compare perfectly, but I can’t compare all positions... That’s the case:

foreach($periodo as $data){
    $arrayDiasSemestre[] = $data->format("w");
}          

include_once 'classes/professoresclass.php';    

if(!isset($_SESSION)){ session_start(); } 

$idSemestre = $_SESSION['SemestreGeral'];

$oProfessor = new professoresclass();
$oProfessor ->listarDisponibilidade4($idSemestre);

while ($arrayDisp = mysql_fetch_array($oProfessor->retorno())){
    if ($arrayDisp['dia'] == $arrayDiasSemestre[]) {
        echo $arrayDisp['idP'];
    }
}

The problem is the $arrayDiasSemestre[]... How could I make it go through all the positions of this array by comparing item by item?

  • 2

    What you have $arrayDisp['dia']? Can you give an example of content?

  • 3

    I still think it was worth answering the question in the comments, to clarify the question.

2 answers

3


Place a foreach after the while to make the comparison of each line that comes with the bank for each value of $arrayDiasSemestre.

while ($arrayDisp = mysql_fetch_array($oProfessor->retorno())){
    foreach($arrayDiasSemestre as $item){
       if ($arrayDisp['dia'] == $item) {
          echo $arrayDisp['idP'];
    }
}

You can also use the function in_array() to resume a Boolean if the value exists or not.

while ($arrayDisp = mysql_fetch_array($oProfessor->retorno())){
   if(in_array($arrayDisp['dia'], arrayDiasSemestre)){
      echo $arrayDisp['idP'];
   }
}
  • +1 I think the way is this, using the in_array()

  • It worked with foreach! the in_array gave way maybe pq the comparison item is another array hence gave error, but as the foreach worked then blz! Thanks!!

1

If I understand correctly, compare perfectly would compare key and value of an array. For this, you can use array_diff_assoc for compute the difference between arrays with additional index checking, to check values and keys of an array.

1) An array with unexpected value: output: array( [1] => value4 )

$_array = array( 'value1' , 'value2' , 'value3' );
$arrays = array( 'value1' , 'value4' , 'value3' );
$result = array_diff_assoc( $arrays , $_array );
print_r( $result );

2) An array with values out of order: output: array( [1] => value3 , [2] => value2 )

$_array = array( 'value1' , 'value2' , 'value3' );
$arrays = array( 'value1' , 'value3' , 'value2' );
$result = array_diff_assoc( $arrays , $_array );
print_r( $result );

3) An array with equal keys and values: output: array()

$_array = array( 'value1' , 'value2' , 'value3' );
$arrays = array( 'value1' , 'value2' , 'value3' );
$result = array_diff_assoc( $arrays , $_array );
print_r( $result );

Example in ideone .


DOC: array_diff_assoc( array $array1 , array $array2 [, array $... ] )

Compares array1 with array2 and returns the difference. Unlike array_diff(), keys of arrays are used in comparison.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.