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?
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 ------ 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)); } 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. - SergiksSource: https://ru.stackoverflow.com/questions/519898/
All Articles
$session = [ 'a' => ['A', 'B', 'C'], 'b' => ['A', 'B', 'CCCP'], 'c' => ['A', 'B', 'C'], 'd' => ['A', 'BBBB', 'C'], ];- Sergiks$sessionsorted, and compare only alternately with neighbors? For example the differences between "a" and "d" are no longer interesting to anyone? - Sergiks