Hướng dẫn how to compare single value with array in php - cách so sánh giá trị đơn với mảng trong php

Làm thế nào để so sánh một giá trị với mỗi giá trị duy nhất trong mảng cụ thể?

Ví dụ:

$list = ("blue", "red", "green", "yellow", "orange", "white"); $value = "blue"; if ($value == $list) { // then print "This is BLUE"; }

Tôi chỉ cần một lần để xác định màu sắc, không cần lặp lại để in màu khác.

Điều này có thể mang lại cho tôi để thực hiện một giá trị động được truy xuất từ ​​cơ sở dữ liệu và sau đó so sánh một mảng [mã cứng] trong tập lệnh PHP. Sau đó, trả lại giá trị chính xác phù hợp với màn hình, ví dụ như kịch bản trên.

(Php 4> = 4.0.1, Php 5, Php 7, Php 8)

mảng_diff - tính toán sự khác biệt của mảngComputes the difference of arrays

Sự mô tả

mảng_diff (mảng $array, mảng ...$arrays): mảng(array $array, array ...$arrays): array

Thông số

array

Mảng để so sánh từ

arrays

Mảng để so sánh với

Trả về giá trị

Trả về một mảng chứa tất cả các mục từ array không có trong bất kỳ mảng nào khác. Chìa khóa trong mảng array được bảo quản.array containing all the entries from array that are not present in any of the other arrays. Keys in the array array are preserved.

Thay đổi

Phiên bảnSự mô tả
8.0.0 mảng_diff (mảng $array, mảng ...$arrays): mảng

Thông số

arrayarray_diff() example

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>

Mảng để so sánh từ

arraysarray_diff() example with non-matching types

Mảng để so sánh với

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;

$array0

$array1

Trả về giá trịarray_udiff().

Trả về một mảng chứa tất cả các mục từ array không có trong bất kỳ mảng nào khác. Chìa khóa trong mảng array được bảo quản.

Thay đổi:

Phiên bản

Hàm này bây giờ có thể được gọi chỉ với một tham số. Trước đây, ít nhất hai tham số đã được yêu cầu.

  • Ví dụ
  • Ví dụ #1 Array_diff () ví dụ
  • Nhiều lần xuất hiện trong $ Array1 đều được xử lý theo cùng một cách. Điều này sẽ xuất hiện:
  • Ví dụ #2 Array_diff () Ví dụ với các loại không phù hợp

Hai yếu tố được coi là bằng nhau khi và chỉ khi (string) $elem1 === (string) $elem2. Đó là, khi biểu diễn chuỗi là như nhau.

Để sử dụng hàm so sánh thay thế, hãy xem Array_udiff ().

$array3

$array4

$array5

$array6

$array7

$array8

Ghi chú

Ghi chú:

$array9

...$arrays0

$array8

Hàm này chỉ kiểm tra một chiều của mảng N chiều. Tất nhiên bạn có thể kiểm tra kích thước sâu hơn bằng cách sử dụng $array2.

Xem thêm

...$arrays2

...$arrays3

$array8

Array_Diff_assoc () - Tính toán chênh lệch của mảng với kiểm tra chỉ mục bổ sung

Array_udiff () - Tính toán sự khác biệt của mảng bằng cách sử dụng hàm gọi lại để so sánh dữ liệu

...$arrays5

...$arrays6

...$arrays7

...$arrays8

...$arrays9

array0

array1

array2

array3

Array_Intersect () - Tính toán giao điểm của mảng

Array_intersect_assoc () - Tính toán giao điểm của các mảng bằng kiểm tra chỉ mục bổ sung

array4

array5

array6

array7

nilsandre tại gmx dot de ¶

15 năm trước

array8

array9

arrays0

arrays1

Ẩn danh ¶

16 năm trước

arrays2

James Dot plznospam tại Bush Dot CC ¶

5 năm trước

arrays3

arrays4

arrays5

arrays6

Firegun tại terra dot com dot br ¶

13 năm trước

arrays7

arrays8

arrays9

array0

array1

array2

$array8

Jeppe Utzon ¶

Xem thêm

array4

array5

array6

$array8

Ghi chú

Ghi chú:

array8

array9

array0

array1

array2

$array8

Hàm này chỉ kiểm tra một chiều của mảng N chiều. Tất nhiên bạn có thể kiểm tra kích thước sâu hơn bằng cách sử dụng $array2.

Xem thêm

array4

array5

array6

array7

array8

$array8

Ghi chú

Ghi chú:

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
0

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
1

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
2

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
3

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
4

Hàm này chỉ kiểm tra một chiều của mảng N chiều. Tất nhiên bạn có thể kiểm tra kích thước sâu hơn bằng cách sử dụng $array2.

Xem thêm

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
5

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
6

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
7

$array8

Array_Diff_assoc () - Tính toán chênh lệch của mảng với kiểm tra chỉ mục bổ sung

Xem thêm

<?php
$array1 
= array("a" => "green""red""blue""red");
$array2 = array("b" => "green""yellow""red");
$result array_diff($array1$array2);print_r($result);
?>
9

(string) $elem1 === (string) $elem20

$array8

Array_Diff_assoc () - Tính toán chênh lệch của mảng với kiểm tra chỉ mục bổ sung

Xem thêm

(string) $elem1 === (string) $elem22

Array_Diff_assoc () - Tính toán chênh lệch của mảng với kiểm tra chỉ mục bổ sung

Ghi chú:

(string) $elem1 === (string) $elem23

(string) $elem1 === (string) $elem24

$array8

Hàm này chỉ kiểm tra một chiều của mảng N chiều. Tất nhiên bạn có thể kiểm tra kích thước sâu hơn bằng cách sử dụng $array2.

Xem thêm

(string) $elem1 === (string) $elem26

Array_Diff_assoc () - Tính toán chênh lệch của mảng với kiểm tra chỉ mục bổ sung

Array_udiff () - Tính toán sự khác biệt của mảng bằng cách sử dụng hàm gọi lại để so sánh dữ liệu

(string) $elem1 === (string) $elem27

(string) $elem1 === (string) $elem28

(string) $elem1 === (string) $elem29

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
0

Array_Intersect () - Tính toán giao điểm của mảng

Ghi chú:

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
1

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
2

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
3

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
4

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
5

$array8

Ghi chú

Array_udiff () - Tính toán sự khác biệt của mảng bằng cách sử dụng hàm gọi lại để so sánh dữ liệu

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
7

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
8

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1234];
$filter = [34, [5], 6];
$result array_diff($source$filter);// Whereas this is fine, since the objects can cast to a string.
class {
  private 
$v;
9

$array00

$array01

$array02

$array03

$array04

$array05

$array06

$array07

$array08

$array09

$array10

$array11

$array12

$array13

$array14

$array15

$array16

$array17

$array18

$array19

$array20

$array21

$array22

$array23

$array24

$array25

$array26

$array8

Coder Viking ¶

16 năm trước

$array28

$array29

$array30

$array31

$array8

Ẩn danh ¶

7 năm trước

$array33

$array34

$array35

$array8

Gilthans tại Nogmailspam dot com ¶

15 năm trước

$array37

$array38

$array39

$array8

J dot j dot d dot mol at ewi dot tudelft dot nl ¶

17 năm trước

$array41

$array42

$array43

$array44

DS2U tại hotmail dot com

19 năm trước

$array45

$array46

$array47

$array8

Làm thế nào để bạn kiểm tra xem giá trị có tồn tại trong một mảng PHP không?

Hàm in_array () là một hàm sẵn có trong PHP được sử dụng để kiểm tra xem một giá trị nhất định có tồn tại trong một mảng hay không. Nó trả về đúng nếu giá trị đã cho được tìm thấy trong mảng đã cho và sai.in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.

Làm thế nào tôi có thể so sánh một mảng với một mảng khác trong PHP?

Hàm mảng_diff () so sánh các giá trị của hai mảng (hoặc nhiều) và trả về sự khác biệt.Hàm này so sánh các giá trị của hai mảng (hoặc nhiều) và trả về một mảng chứa các mục từ Array1 không có trong Array2 hoặc Array3, v.v.array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.

Có nghĩa là gì trong PHP?

=> là dấu phân cách cho các mảng kết hợp.Trong bối cảnh của vòng lặp foreach đó, nó gán khóa của mảng cho $ người dùng và giá trị cho $ pass.the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user and the value to $pass .

Làm cách nào để kiểm tra xem hai mảng có cùng một phần tử trong PHP không?

Bây giờ, để kiểm tra xem hai mảng có bằng hay không, một lần lặp có thể được thực hiện trên các mảng và kiểm tra xem mỗi chỉ mục có giá trị liên quan đến chỉ mục trong cả hai mảng có giống nhau hay không.PHP có toán tử mảng sẵn có (===) để kiểm tra giống nhau nhưng ở đây, thứ tự các phần tử mảng không quan trọng.an iteration can be done over the arrays and check whether for each index the value associated with the index in both the arrays is the same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements is not important.

Chủ đề