Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the number of different elements of an array by php

Shulou Source: shulou.com Published: 2022-06-01 11:49:00 10月03日 Update

This article introduces the relevant knowledge of "how to obtain the number of different elements of an array by php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Get method: 1, use array_unique () function to remove repeated elements in the array, so that each element in the array has only one, syntax "array_unique ($arr)"; 2, use sizeof () to get the number of elements of the deduplicated array, syntax "sizeof (deduplicated array)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php gets the number of different elements in the array

Realize the idea:

To get the number of different elements in an array, you first need to remove the repeating elements in the array-- using array_unique ()

Then get the number of elements of the deduplicated array, that is, the length of the array-- using sizeof ()

Implementation code:

Description:

The array_unique () function removes duplicate values from the array and returns an array of results. When the values of several array elements are equal, only the first element is retained and the other elements are deleted. The key name in the returned array remains unchanged.

The sizeof () function can count the number of all elements in an array, or the number of attributes in an object.

That's all for "how to get the number of different elements in an array by php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Array element number different function content more knowledge grammar utility learning and then code only dilemma reality object attribute thought situation. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info vpn Linux macOS MariaDB