繁体   English   中英

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

[英]How to check if pair of values in array of pairs of values in PHP

假设我们有一个对象,其尺寸$height$width 而不是这样做:

if(
   ($height = 500 && $width = 400)
|| ($height = 200 && $width = 380)
|| ($height = 850 && $width = 780)
|| ...
) { ...

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

[ [500,400], [200,380] [850, 780] ]

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";

演示

你可以做这样的事情

 $widths=[500,200,850];
 $heights=[400,380,780];
 if(in_array($width,$widths) && in_array($height,$heights)){
 ...
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM