There is one session array, inside this session array there are keys and arrays corresponding to these keys,

The question is how in the session array to find the difference of values?

  • Specify what will be a “divergence” here: $session = [ 'a' => ['A', 'B', 'C'], 'b' => ['A', 'B', 'CCCP'], 'c' => ['A', 'B', 'C'], 'd' => ['A', 'BBBB', 'C'], ]; - Sergiks
  • This means that you need to remove C from the key b of the CCCP from the key b. Then the key 'c' value differs from the key 'b' and therefore it is necessary to output and the key d remains the value of the BBBB diverges from the previous value from the key 'c' really do? After all, you need to follow the history of data changes - Ilenya
  • Those. to consider $session sorted, and compare only alternately with neighbors? For example the differences between "a" and "d" are no longer interesting to anyone? - Sergiks
  • Yes, yes, even so, this will be enough for me - Ilenya

2 answers 2

After the clarifying comments a completely different picture was drawn. We compare only the current with the following. And, because need a difference on both sides, then sort both, and move one by one, comparing:

 <?php $session = [ 'a' => ['A', 'B', 'C'], 'b' => ['A', 'B', 'CCCP'], 'c' => ['A', 'B', 'C'], 'd' => ['A', 'BBBB', 'C'], ]; $result = []; $keys = array_keys($session); for( $i = 0; $i < count($keys)-1; $i++) { $A = $session[ $keys[ $i]]; $B = $session[ $keys[ $i+1]]; printf( "%d [%s] [%s]\n", $i, implode(',', $A), implode( ',', $B)); sort( $A); sort( $B); for( $ai=0, $bi=0; $ai < count($A) && $bi < count($B); $ai++, $bi++) { if( $A[$ai] === $B[$bi]) { continue; } else if( $A[$ai] < $B[$bi]) { $bi--; echo "A not in B: " . $A[$ai] . PHP_EOL; } else if( $A[$ai] > $B[$bi]) { $ai--; echo "B not in A: " . $B[$bi] . PHP_EOL; } } if( $ai < count($A)) { echo "A not in B: " . implode(',', array_slice( $A, $ai)) . PHP_EOL; } else if( $bi < count($B)) { echo "B not in A: " . implode(',', array_slice( $B, $bi)) . PHP_EOL; } echo '------' . PHP_EOL; } 

At the exit:

 0 [A,B,C] [A,B,CCCP] A not in B: C B not in A: CCCP ------ 1 [A,B,CCCP] [A,B,C] B not in A: C A not in B: CCCP ------ 2 [A,B,C] [A,BBBB,C] A not in B: B B not in A: BBBB ------ 
  • Thank you, with the comments above, I'm getting closer to completing the task, but still not at all. Imagine that there are archival data and with the help of the question in the topic header you need to find the discrepancy that is the changing data - Ilenya

There is a built-in function .

 $array1 = array("a" => "green", "red", "blue", "red"); $array2 = array("b" => "green", "yellow", "red"); $result = array_diff($array1, $array2); print_r($result); 

Result:

 Array ( [1] => blue ) 

UPDATE:

 foreach($_SESSION as $el1) foreach($_SESSION as $el2) { print_r(array_diff($el1, $el2)); } 
  • Here you are, I need to check for discrepancy in ONE array, and you have array1 and array2 - Ilenya
  • And what's stopping you from going through two cycles on ONE array to compare the elements in pairs? - A1essandro
  • foreach..foreach - this is how (n^2+n)/2 unnecessary checks are performed: each pair is compared twice + each array is compared once to itself. - Sergiks