Mavjud qatordan yangi qatorni oldingi qatordan takrorlangan qiymat va kalitlarni qayta hisoblash qiymati bilan almashtirmoqchiman?

Mening eski qatorim

array:7 [
  0 => "22-Feb-2017"
  1 => "22-Feb-2017"
  2 => "22-Feb-2017"
  3 => "27-May-2015"
  4 => "10-May-2015"
  5 => "10-May-2015"
  6 => "08-May-2015"
]

Yangi qatorni istayman

array:7 [
  22-Feb-2017 => "3"
  27-May-2015 => "1"
  10-May-2015 => "2"
  08-May-2015 => "1"
]
0

6 javoblar

array_count_values ​​ dan foydalaning. array_count_values ​​() funktsiyasi qatorning barcha qiymatlarini hisoblaydi.

<?php
$dates = [
    0 => "22-Feb-2017",
    1 => "22-Feb-2017",
    2 => "22-Feb-2017",
    3 => "27-May-2015",
    4 => "10-May-2015",
    5 => "10-May-2015",
    6 => "08-May-2015",
];
$final_array = array_count_values($dates);
print_r($final_array);

Chiqish

Array
(
    [22-Feb-2017] => 3
    [27-May-2015] => 1
    [10-May-2015] => 2
    [08-May-2015] => 1
)
6
qo'shib qo'ydi
thasnk !! Bu men uchun ishlaydi
qo'shib qo'ydi muallif Jagdish Chaudhary, manba

Siz shunga o'xshash biror narsa qidirayotganingizni bilmasangiz

http://php.net/manual/en/function.array-count -values.php

5
qo'shib qo'ydi
javob uchun rahmat
qo'shib qo'ydi muallif Jagdish Chaudhary, manba

Quyidagi chiqimni tekshiring:

 $a=array(0 => "22-Feb-2017",
    1 => "22-Feb-2017",
    2 => "22-Feb-2017",
    3 => "27-May-2015",
    4 => "10-May-2015",
    5 => "10-May-2015",
    6 => "08-May-2015");

    ///array_count_values counts the same values count////
    $new_array = array_count_values($a);
/// now let's interchange the keys and values////
    foreach($new_array as $key=>$value){
        $out[$value] = $key;
    }

    print_r($out);
2
qo'shib qo'ydi
Rahmat! Ushbu asarlar
qo'shib qo'ydi muallif Jagdish Chaudhary, manba
$data=[0 => "22-Feb-2017",1 => "22-Feb-2017",2 => "22-Feb-2017",3 => "27-May-2015",4 => "10-May-2015",5 => "10-May-2015",6 => "08-May-2015"];
$a=array_count_values($data);

var_dump($a);
1
qo'shib qo'ydi
javob uchun rahmat
qo'shib qo'ydi muallif Jagdish Chaudhary, manba

Sizga aynan shu narsa kerak:

  <?php
    $old_array=array(
        0 => "22-Feb-2017",
        1 => "22-Feb-2017",
        2 => "22-Feb-2017",
        3 => "27-May-2015",
        4 => "10-May-2015",
        5 => "10-May-2015",
        6 => "08-May-2015");

        /*
        Counting the occurances of value & storing it in a new array in the format:
        array("22-Feb-2017"=>3, "27-May-2015"=>2, .........)
        */
        $new_array = array_count_values($a);

        print_r($new_array);

        echo $new_array["22-Feb-2017"];
    ?>
1
qo'shib qo'ydi
javob uchun rahmat
qo'shib qo'ydi muallif Jagdish Chaudhary, manba

Buning bir usuli:

$dates = [
    0 => "22-Feb-2017",
    1 => "22-Feb-2017",
    2 => "22-Feb-2017",
    3 => "27-May-2015",
    4 => "10-May-2015",
    5 => "10-May-2015",
    6 => "08-May-2015",
];

$result = [];

foreach ($dates as $date) {
    $result[$date] = array_key_exists($date, $result) ? $result[$date] + 1 : 1;
}

print_r($result);
1
qo'shib qo'ydi
Rahmat! Bu asarlar
qo'shib qo'ydi muallif Jagdish Chaudhary, manba
Laravel Uzbek Community
Laravel Uzbek Community
413 ishtirokchilar

PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin