I have an array of the form:

array ( 0 => array ( 'ID' => '532', 'LASTNAME' => 'Henigan', 'FIRSTNAME' => 'John', 'BDAY' => 'Jan 24, 1998', 'CLASS' => '[5] CL2', 'CLASS_PRICE' => '200.00', 'PID' => '530', 'GCID' => '5', 'AMOUNT' => '200.00', 'billing_name' => 'BettyAnne Henigan', 'parent_tuition' => '0.00', 'DISCOUNT' => '0.00', 'DISCOUNT_TYPE' => '0', 'DISCOUNT_MODE' => '0', ), 1 => array ( 'ID' => '612', 'LASTNAME' => 'Frantz', 'FIRSTNAME' => 'Alan', 'BDAY' => 'Mar 2, 1992', 'CLASS' => '[4] CL1', 'CLASS_PRICE' => '100.00', 'PID' => '611', 'GCID' => '4', 'AMOUNT' => '100.00', 'billing_name' => 'Bridget Frantz', 'parent_tuition' => '0.00', 'DISCOUNT' => '0.00', 'DISCOUNT_TYPE' => '0', 'DISCOUNT_MODE' => '0', ), 

and so on.

How to count the number of 'PID' values ​​with the same values?

    2 answers 2

    Option 1

     $arrayCounter = []; //Посчитаем кол-во повторений foreach ($array as $item) { $pid = $item['PID']; if (!isset($arrayCounter[$pid])) { $arrayCounter[$pid] = 1; } else { $arrayCounter[$pid]++; } } //Получим все, которые повторяются больше 1 раза $result = array_filter($arrayCounter, function($var) { return $var>1; }); var_dump($result); 

    Option 2

     $tempArray = []; foreach ($array as $item) { $tempArray[] = $item['PID']; } $result = array_filter(array_count_values($tempArray), function($var) { return $var>1; }); var_dump($result); 

    Displays the PID (key) and the number of repetitions (value), if you need to get only the keys (PID), then you need to run the result through the function array_keys;

        foreach ($response['data'] as $item){ $count[$item['ID']] += 1; }