Reputation: 653
I have an array thats returned form a query showing me all bookings in a certain time period. I'm trying to find the most common treatment in this array. The treatment is in the array as idtreatment. How would I even begin to search through an array to find the most common value of idtreatment? I have tried using
$return = array();
foreach ($treatment as $a)
{
foreach ($treatment as $b)
{
if ($a === $b) continue;
$return = array_merge($return, array_intersect($a, $b));
}
}
where $treatment is the array below but it just seems to include all the other common values rather than just looking at idtreatment and only taking that into account
Array
(
[0] => Array
(
[idbooking] => 30255
[idtreatment] => 65
[idtreatmentLocation] => 1
[iduser] => 632
[idstylist] => 65
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 1
[bookingDate] => 2017-01-05
[bookingSTime] => 09:00:00
[bookingCreated] => 2017-01-02 18:41:43
[bookingETime] => 09:30:00
[bookingToken] => GAsW3k8ASEQo7iS1SScIrt7lB9l31ZHHAaDCvKdnp4LvlDwewIXXb5i
[bookingComplete] => 1
[bookingPrice] => 20
[bookingRebooked] => 0
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 20
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Shellac Fingers
)
[1] => Array
(
[idbooking] => 28849
[idtreatment] => 168
[idtreatmentLocation] =>
[iduser] => 214
[idstylist] => 73
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 1704
[bookingDate] => 2017-01-05
[bookingSTime] => 09:00:00
[bookingCreated] => 2016-11-16 18:44:55
[bookingETime] => 09:45:00
[bookingToken] => cCdXCwqAR7xa75CBwr3LvSxCWjC2YKHamU8YrxziE7tLa6mHwos3h1F
[bookingComplete] => 1
[bookingPrice] => 20
[bookingRebooked] => 1
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 20
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Shellac Fingers & Remove
)
[2] => Array
(
[idbooking] => 30253
[idtreatment] => 65
[idtreatmentLocation] =>
[iduser] => 334
[idstylist] => 65
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 70
[bookingDate] => 2017-01-05
[bookingSTime] => 09:30:00
[bookingCreated] => 2016-12-31 14:37:14
[bookingETime] => 10:00:00
[bookingToken] => 4if86rm0vsxbqIf2V20Xu4XHCKJeZ3U3k0aqBwz7Z8iqPysKzBI5FGM
[bookingComplete] => 1
[bookingPrice] => 20
[bookingRebooked] => 0
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 20
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Shellac Fingers
)
[3] => Array
(
[idbooking] => 29512
[idtreatment] => 63
[idtreatmentLocation] =>
[iduser] => 330
[idstylist] => 73
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 65
[bookingDate] => 2017-01-05
[bookingSTime] => 09:45:00
[bookingCreated] => 2016-12-07 17:36:04
[bookingETime] => 10:00:00
[bookingToken] => QMHxMb5PkACXcUWxpDBbIoV8zy6up9ufUcMHoRwIr9FE4CcsfNEXcz6
[bookingComplete] => 1
[bookingPrice] => 10
[bookingRebooked] => 1
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 10
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Eyebrow Thread
)
[4] => Array
(
[idbooking] => 29513
[idtreatment] => 436
[idtreatmentLocation] =>
[iduser] => 330
[idstylist] => 73
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 65
[bookingDate] => 2017-01-05
[bookingSTime] => 10:00:00
[bookingCreated] => 2016-12-07 17:36:21
[bookingETime] => 10:15:00
[bookingToken] => JkFvtRL2avOptokPQMTKnEh2129CzTcidSNGJyIU5wjyVEnMrgwPUOR
[bookingComplete] => 1
[bookingPrice] => 0
[bookingRebooked] => 1
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 0
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Face Mapping - 15 Mins
)
[5] => Array
(
[idbooking] => 29511
[idtreatment] => 65
[idtreatmentLocation] =>
[iduser] => 330
[idstylist] => 73
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 65
[bookingDate] => 2017-01-05
[bookingSTime] => 10:15:00
[bookingCreated] => 2016-12-07 17:35:35
[bookingETime] => 10:45:00
[bookingToken] => qVLGE2zwPpPXexiZCs8WVQuOlHQRpQGQMswru7XkwMhKkAJX4QTZHoO
[bookingComplete] => 1
[bookingPrice] => 20
[bookingRebooked] => 30380
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 20
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Shellac Fingers
)
[6] => Array
(
[idbooking] => 28867
[idtreatment] => 206
[idtreatmentLocation] =>
[iduser] => 3474
[idstylist] => 65
[idbranch] => 1
[idStatus] => 1
[idsystem] => 1
[bookingCreatedBy] => 65
[bookingDate] => 2017-01-05
[bookingSTime] => 10:30:00
[bookingCreated] => 2016-11-17 10:28:49
[bookingETime] => 10:45:00
[bookingToken] => ZVapypjdIrENXPyvi9fTuKvEWKgCE6gE2q4zQmNyOrlLgUgy4wsxhXc
[bookingComplete] => 1
[bookingPrice] => 17
[bookingRebooked] => 1
[bookingPatchTest] => 0
[bookingPriceAmend] => 0
[bookingTreatPrice] => 17
[bookingLink] => 0
[isLunch] => 0
[treatmentName] => Eyebrow Thread
)
)
Upvotes: 0
Views: 30
Reputation: 35200
Going off your $treatment
array if you want to the most common occurrence of idtreatment
you could do something like:
$idTreatments = [];
foreach ($treatment as $value) {
$id = $value['idtreatment'];
if (!isset($idTreatments[$id])) {
$idTreatments[$id] = 0;
}
$idTreatments[$id]++;
}
arsort($idTreatments);
$commonTreatment = key($idTreatments);
If you're using PHP 5.5+ you could use array_column
and array_count_values
instead of the foreach
loop:
$idTreatments = array_count_values(array_column($treatment, 'idtreatment'));
Hope this helps!
Upvotes: 2