How to get different values ​​from array in php

青灯夜游
Release: 2023-03-14 22:20:01
Original
1850 people have browsed it

Value method: 1. Use the "array_unique(array)" statement to remove duplicate values ​​in the array and return an array containing different values; 2. Use the "array_keys(array_count_values(array))" statement to remove duplicates Duplicate values ​​in an array, returns an array containing distinct values.

How to get different values ​​from array in php

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

php takes from the array Different values ​​

1. Use the array_unique() function:

array_unique() function is used to remove duplicates in the array value. If two or more array values ​​are the same, only the first value is retained and the other values ​​are removed.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("A","Cat","Dog","A","Dog",1,2,3,3,2,1);
echo "原数组:";
var_dump($arr);
echo "获取数组中的不同值:";
var_dump(array_unique($arr));
?>
Copy after login

How to get different values ​​from array in php

2. Use array_keys() and array_count_values() functions

array_count_values() function is used to count all values ​​in the array The number of occurrences. The key name in the returned array is the value of the original array, and the key value is the number of times the value appears in the original array.

array_keys() function returns a new array containing all the key names in the array.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array("A","Cat","Dog","A","Dog",1,2,3,3,2,1);
echo "原数组:";
var_dump($arr);
echo "获取数组中的不同值:";
var_dump(array_keys(array_count_values($arr)));
?>
Copy after login

How to get different values ​​from array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get different values ​​from array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template