php - Find repeating values in multidimensional array -
i'm trying find when array has 2nd dimension values same can deal them.
i've looked @ array_unique
, other people asking similar question, delete values instead of returning them.
say have array this:
array( [0] => array( [laps] => 7, [corrected_time] => 18 ), [1] => array( [laps] => 6, [corrected_time] => 18 ), [2] => array( [laps] => 7, [corrected_time] => 18.5 ) )
i'd have return: array(0,1)
because both have same value corrected time
here 1 approach. first values corrected_time
, convert them strings (because we'll use them in array_count_values
, works on ints , strings).
$times = array_map('strval', array_column($your_array, 'corrected_time'));
then find values occur more once using array_count_values
, array_filter
.
$repeats = array_filter(array_count_values($times), function($time) { return $time > 1; });
after have list of repeated times, can use
filter original array include items repeated times.
$multiples = array_filter($your_array, function($item) use ($repeats){ return isset($repeats[(string) $item['corrected_time']]); });
you can iterate on this, or if want keys, can them with
$keys = array_keys($multiples);
Comments
Post a Comment