菜单

PHP searches for only one digit in the array to find a specific element

2017年11月23日 - PHPer

In this paper, an example is given to illustrate the implementation of PHP to find only one digit in the array. To share with you for your reference, as follows:

Problem:

In an integer array, except for two numbers, the other numbers appear two times. Please write a program to find out the two digits that appear only once.

The code is as follows:

<? PHP

Function FindNumsAppearOnce ($array)

{

Code here / / write

Return / list, such as [a, b], AB which is two times the number one

$count = array_count_values ($array);

Foreach ($count as $k=> $v) {

If ($v = = 1) {

$new_arr[] = $k;

}

}

Return $new_arr;

}

$arr=array (’22’,’44’,’66’,’11’,’11’,’44’,’33’);

Print_r (FindNumsAppearOnce ($arr));

Output:

Array

(

[0] => 22

[1] => 66

[2] => 33

)

发表评论

电子邮件地址不会被公开。