How to compare all the positions of an array in PHP [closed]

0

I filled in my two arrays that I need to compare perfectly, but I can not compare all the positions ... This is 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 $arrayDiasSemestre[] ... How could I make it go through all the positions of this array by comparing item by item?

    
asked by anonymous 29.11.2014 / 21:18

2 answers

3

Put a foreach after the while to compare 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 in_array () function that retries a boolean if the value exists or no.

while ($arrayDisp = mysql_fetch_array($oProfessor->retorno())){
   if(in_array($arrayDisp['dia'], arrayDiasSemestre)){
      echo $arrayDisp['idP'];
   }
}
    
29.11.2014 / 21:35
1

If I understood 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 the values and keys of an array.

1) An array with an 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 out-of-order values: 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 on ideone .

  

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

     

Compare array1 with array2 and return the difference. Unlike array_diff (), arrays keys are used in the comparison .

    
29.11.2014 / 23:27