# 在给定的数组中查找一个数_PHP | 查找数组中给定元素的出现

Given an array and an element, we have to find the occurrences of the element in the array.

To find the occurrences of a given element in an array, we can use two functions,

1. array_keys()

array_keys()

It returns an array containing the specified keys (values).

它返回一个包含指定键(值)的数组。

2. count()

计数()

It returns the total number of elements of an array i.e. count of the elements.

它返回数组中元素的总数，即元素数。

Firstly, we will call array_keys() function with the input array (in which we have to count the occurrences of the specified element) and element, array_keys() will return an array containing the keys, then we will call the function count() and pass the result of the array_keys() which will finally return the total number of occurrences of that element.

PHP code to find the occurrences of a given element

PHP代码查找给定元素的出现

<?php
//an array of the string elements
$array = array( "The", "Quick", "Brown", "Fox", "Jumps", "Right", "Over", "The", "Lazy", "Dog" ); //word/element to find the the array$word= "The";
//finding the occurances
$wordCount=count(array_keys($array, $word)); //printing the result echo "Word <b>" .$word ."</b> Appears " .$wordCount ." time(s) in array\n"; //word/element to find the the array$word= "Hello";
//finding the occurances
$wordCount=count(array_keys($array, $word)); //printing the result echo "Word <b>" .$word ."</b> Appears " .$wordCount ." time(s) in array\n"; ?>   Output 输出量 Word The Appears 2 time(s) in array Word Hello Appears 0 time(s) in array   Explanation: 说明： Here, we have an array$array with the string elements and then assigning the element to $word to find its occurrences, as mentioned the above example, element "The" appears two times in the$array and element "Hello" doesn't appear.

12-19 4382

07-24 309
08-15 389
07-01 2481
05-01 203
03-20 4348
04-21 2533
03-18 681
03-13 2706
05-14 9681
07-05 2350
07-06 1766
02-04 2万+
10-07 1608
03-22 777
05-23 1354
12-11 7926