php - How to find values which were found before inside multi array -
i have following problem. have check if array contains exact same values , if found before.
int(3) wasn´t found before 0, int(8) wasn´t found before 0, int(5) wasn´t found before 0, int(8) found before 1, int(3) , int(8) not found 0, , on.
i tried array_unique didn´t work wanted
for example:
array(7) { [2] => array(1) { [0] => int(3) } [3] => array(1) { [0] => int(8) } [4] => array(1) { [0] => int(5) } [5] => array(1) { [0] => int(8) } [6] => array(2) { [0] => int(3) [1] => int(8) } [7] => array(2) { [0] => int(2) [1] => int(5) } [8] => array(2) { [0] => int(3) [1] => int(8) } } it must this
array(7) { [2] => array(1) { [0] => int(0) } [3] => array(1) { [0] => int(0) } [4] => array(1) { [0] => int(0) } [5] => array(1) { [0] => int(1) } [6] => array(1) { [0] => int(0) } [7] => array(1) { [0] => int(0) } [8] => array(1) { [0] => int(1) } }
you use array_map() , serialize():
<?php $data = [ 2 => [ 3, ], 3 => [ 8, ], 4 => [ 5, ], 5 => [ 8, ], 6 => [ 3, 8, ], 7 => [ 2, 5, ], 8 => [ 3, 8, ], ]; $occurrences = []; $mapped = array_map(function (array $values) use (&$occurrences) { // create serialized representation of values // can use index $index = serialize($values); // haven't seen these values before if (!array_key_exists($index, $occurrences)) { $occurrences[$index] = 1; return 0; } // increase our counter $occurrences[$index]++; return $occurrences[$index] - 1; }, $data); var_dump($mapped); for reference, see:
for example, see:
Comments
Post a Comment