如何在PHP中检查值对数组中的值对

e_i*_*_pi 0 php arrays

假设我们有一个具有尺寸$height和尺寸的物体$width.而不是这样做:

if(
   ($height = 500 && $width = 400)
|| ($height = 200 && $width = 380)
|| ($height = 850 && $width = 780)
|| ...
) { ...
Run Code Online (Sandbox Code Playgroud)

...在PHP中是否存在"速记"(即人类易于阅读和维护)的方式来检查数组[$height, $width]是否在以下数组数组中?

[ [500,400], [200,380] [850, 780] ]
Run Code Online (Sandbox Code Playgroud)

Mar*_*vin 5

in_array 可以使用数组:

<?php
$dimensions = [[500, 400], [200, 380], [850, 780]];

$needle1 = [500, 400];
$needle2 = [500, 440];

echo "needle1 in array: ".in_array($needle1, $dimensions)."\n";
echo "needle2 in array: ".in_array($needle2, $dimensions)."\n";
Run Code Online (Sandbox Code Playgroud)

(演示)