Php đếm giá trị cụ thể trong mảng

Array_count_values[] là một hàm có sẵn trong PHP được sử dụng để đếm tất cả các giá trị bên trong một mảng. Nói cách khác, chúng ta có thể nói rằng hàm array_count_values[] được sử dụng để tính tần số của tất cả các phần tử của một mảng

cú pháp

array array_count_values[ $array ]

Thông số. Hàm này chấp nhận một tham số $array. Tham số này là mảng mà chúng ta cần tính tổng số giá trị có trong nó

Giá trị trả về. Hàm này trả về một mảng kết hợp với các cặp khóa-giá trị trong đó các khóa là các phần tử của mảng được truyền dưới dạng tham số và các giá trị là tần suất của các phần tử này trong một mảng

Ghi chú. Nếu phần tử không phải là một chuỗi hoặc số nguyên thì một E_WARNING sẽ được đưa ra

ví dụ

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 

Chương trình dưới đây minh họa hoạt động của hàm array_count_values[] trong PHP




Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
4

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
5

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
6

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
7

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
8
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
9
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
0____10

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
1
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
2
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
3
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
4
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
3
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
0
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
7

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
8

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
5

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
00

Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
0
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
02
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
03
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
3
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
05
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
06
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
07
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
06
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
05
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
06
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
05
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
06
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
43
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
06
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
07
Input : array = ["Geeks", "for", "Geeks", "Geeks", "Welcome", "for"]
Output : 
        Array
        [
          [Geeks] => 3
          [for] => 2
          [Welcome] => 1
        ]

Input : array = [1, 1, 2, 3 , 1 , 2 , 4, 5]
Output :
       Array
       [
         [1] => 3
         [2] => 2
         [3] => 1
         [4] => 1
         [5] => 1
       ] 
46

Array_count_values ​​là gì?

Hàm array_count_values[] được dùng để đếm tất cả các giá trị bên trong một mảng . Nói cách khác, chúng ta có thể nói rằng hàm array_count_values[] được sử dụng để tính tần số của tất cả các phần tử của một mảng. cú pháp. mảng array_count_values[ $array ]

+= có hoạt động trên các mảng trong PHP không?

Toán tử + trong PHP khi được áp dụng cho mảng sẽ thực hiện công việc của mảng UNION . mảng $arr += mảng $arr1; .

Làm cách nào tôi có thể lặp qua một mảng trong PHP?

6 cách lặp qua một mảng trong php .
while[biểu thức]{ // Đoạn mã được thực thi }
do { // Đoạn mã được thực thi } while[biểu thức];
for [expr1; expr2; expr3] { // Code sẽ được thực thi }
mảng_walk[mảng. đối tượng &$array, gọi lại được $arg, hỗn hợp $arg = null]. bool

Chủ Đề