从php数组中最常见的idtreatment

时间:2017-01-08 10:56:46

标签: php arrays

我有一个数组,从查询中返回,向我显示某个时间段内的所有预订。我试图找到这个阵列中最常见的治疗方法。治疗在阵列中作为治疗。我怎么会开始搜索数组来找到最常见的idtreatment值?我尝试过使用

          $return = array();
        foreach ($treatment as $a) 
        {
           foreach ($treatment as $b) 
           {
               if ($a === $b) continue;
               $return = array_merge($return, array_intersect($a, $b));
           }
        } 

其中$ treatment是下面的数组,但它似乎只包含所有其他常见值,而不仅仅是查看iddreatment而只是考虑到这一点

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 

    )
)

1 个答案:

答案 0 :(得分:2)

如果您想要$treatment最常见的情况,请关闭idtreatment数组,您可以执行以下操作:

$idTreatments = [];

foreach ($treatment as $value) {

    $id = $value['idtreatment'];

    if (!isset($idTreatments[$id])) {
        $idTreatments[$id] = 0;
    }

    $idTreatments[$id]++;
}

arsort($idTreatments);

$commonTreatment = key($idTreatments);

如果您使用的是PHP 5.5+,则可以使用array_columnarray_count_values代替foreach循环:

$idTreatments = array_count_values(array_column($treatment, 'idtreatment'));   

希望这有帮助!