假设我们有一个具有尺寸$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)
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)
(演示)
| 归档时间: |
|
| 查看次数: |
53 次 |
| 最近记录: |