Php 我想只使用1个foreach在数组中查找我的值
假设我有一个数组Php 我想只使用1个foreach在数组中查找我的值,php,arrays,Php,Arrays,假设我有一个数组 $myarr = array( '1', '2', '3', '4-7', '9', '10', ) $search2 = '2'; $search5 = '5'; 我想在我的$myarr中检查这两个变量。使用_array中的可以很容易地找到第一个,但是$search5在'4-7'中,但是我该怎么做才能找到值呢?我不想要2个foreach,因为我知道我可以使用分解,然后比较起始值和结束值。是否有一个单线或双线功能可以帮助我实现
$myarr = array(
'1',
'2',
'3',
'4-7',
'9',
'10',
)
$search2 = '2';
$search5 = '5';
我想在我的$myarr
中检查这两个变量。使用_array中的可以很容易地找到第一个
,但是$search5
在'4-7'中,但是我该怎么做才能找到值呢?我不想要2个foreach,因为我知道我可以使用分解
,然后比较起始值和结束值。是否有一个单线或双线功能可以帮助我实现我所需要的?谢谢
尝试以下代码:
$myarr = array('1','2','3','4-7','9','10');
$search = 1; // Try with 5
$flag1 = false;
foreach($myarr as $number){
if(!is_numeric($number)){
list($start,$end) = explode("-",$number);
if($search >=$start && $search <=$end ){
echo $search .' Found';
}
}else if($flag1 == false){
if(in_array($search,$myarr)){
echo $search .' Found';
$flag1 = true;
}
}
}
$myarr=array('1','2','3','4-7','9','10');
$search=1;//试着用5个
$flag1=假;
foreach($myarr作为$number){
如果(!是数字($number)){
列表($start,$end)=分解(“-”,$number);
如果($search>=$start&&$search尝试以下代码:
$myarr = array('1','2','3','4-7','9','10');
$search = 1; // Try with 5
$flag1 = false;
foreach($myarr as $number){
if(!is_numeric($number)){
list($start,$end) = explode("-",$number);
if($search >=$start && $search <=$end ){
echo $search .' Found';
}
}else if($flag1 == false){
if(in_array($search,$myarr)){
echo $search .' Found';
$flag1 = true;
}
}
}
$myarr=array('1','2','3','4-7','9','10');
$search=1;//尝试使用5
$flag1=假;
foreach($myarr作为$number){
如果(!是数字($number)){
列表($start,$end)=分解(“-”,$number);
如果($search>=$start&&$search@Ali Zia,您也可以尝试以下逻辑:
<?php
$myarr = array(
'1',
'2',
'3',
'4-7',
'9',
'10',
);
$flag = 0;
$search = '5';
foreach($myarr as $value){
if($value == $search){
$flag = 1;
break;
}
else if(strpos($value, "-")){
$numberRange = explode("-", $value);
if($numberRange[0] <= $search && $numberRange[1] >= $search){
$flag = 1;
break;
}
}
}
if($flag == 1){
echo "yes number found in the array";
}
else{
echo "sorry not found";
}
@Ali Zia您也可以尝试以下逻辑:
<?php
$myarr = array(
'1',
'2',
'3',
'4-7',
'9',
'10',
);
$flag = 0;
$search = '5';
foreach($myarr as $value){
if($value == $search){
$flag = 1;
break;
}
else if(strpos($value, "-")){
$numberRange = explode("-", $value);
if($numberRange[0] <= $search && $numberRange[1] >= $search){
$flag = 1;
break;
}
}
}
if($flag == 1){
echo "yes number found in the array";
}
else{
echo "sorry not found";
}
另一个答案是:
$myarr = [
'1',
'2',
'3',
'4-7',
'9',
'10',
];
$search2 = '2';
$search5 = '5';
$search12 = '12';
function myInArray($needle, $haystack) {
return in_array(
$needle,
array_reduce(
$haystack,
function($incrementor, $value) {
return array_merge($incrementor, (strpos($value, '-') !== false) ? range(...explode('-', $value)) : [(int)$value]);
},
[]
)
);
}
foreach([$search2, $search5, $search12] as $searchValue) {
echo $searchValue, " is ",(myInArray($searchValue, $myarr) ? '' : 'NOT '), "in the array", PHP_EOL;
}
可能效率不高,尤其是在$myarr
中使用更大范围的值时,但方法略有不同
另一个答案是:
$myarr = [
'1',
'2',
'3',
'4-7',
'9',
'10',
];
$search2 = '2';
$search5 = '5';
$search12 = '12';
function myInArray($needle, $haystack) {
return in_array(
$needle,
array_reduce(
$haystack,
function($incrementor, $value) {
return array_merge($incrementor, (strpos($value, '-') !== false) ? range(...explode('-', $value)) : [(int)$value]);
},
[]
)
);
}
foreach([$search2, $search5, $search12] as $searchValue) {
echo $searchValue, " is ",(myInArray($searchValue, $myarr) ? '' : 'NOT '), "in the array", PHP_EOL;
}
可能效率不高,尤其是在$myarr
中使用更大范围的值时,但方法略有不同
只是为了使用range()
和参数解包,因为其他答案都不能:
function in_array_range($needle, array $haystack) {
if (in_array((int) $needle, $haystack, true)) {
return true;
}
$haystack = array_map(
function($element) {
return strpos($element, '-')
? range(... explode('-', $element, 2))
: (int) $element;
},
$haystack
);
foreach ($haystack as $element) {
if (is_array($element) && in_array((int) $needle, $element, true)) {
return true;
}
}
return false;
}
_数组_范围内的函数($needle,array$haystack){
if(在数组中((int)$needle,$haystack,true)){
返回true;
}
$haystack=阵列\地图(
功能($元素){
返回strpos($element,'-')
?范围(…爆炸(“-”,$element,2))
:(int)$元素;
},
$haystack
);
foreach($haystack作为$element){
if(is_数组($element)和in_数组((int)$needle,$element,true)){
返回true;
}
}
返回false;
}
只是为了使用range()
和参数解包,因为其他答案都没有:
function in_array_range($needle, array $haystack) {
if (in_array((int) $needle, $haystack, true)) {
return true;
}
$haystack = array_map(
function($element) {
return strpos($element, '-')
? range(... explode('-', $element, 2))
: (int) $element;
},
$haystack
);
foreach ($haystack as $element) {
if (is_array($element) && in_array((int) $needle, $element, true)) {
return true;
}
}
return false;
}
_数组_范围内的函数($needle,array$haystack){
if(在数组中((int)$needle,$haystack,true)){
返回true;
}
$haystack=阵列\地图(
功能($元素){
返回strpos($element,'-')
?范围(…爆炸(“-”,$element,2))
:(int)$元素;
},
$haystack
);
foreach($haystack作为$element){
if(is_数组($element)和in_数组((int)$needle,$element,true)){
返回true;
}
}
返回false;
}
这里几乎有一艘班轮
$search = '5';
$matches = explode('-',array_values(preg_grep('/[-]/', $myarr))[0]);
if(in_array($search, $myarr)|| in_array($search,range($matches[0],$matches[1]))){
echo "found";
}else{
echo "not found";
}
我通过-
爆炸,然后用它创建范围,并用in_数组检查它
这里正在工作这里几乎有一条班轮
$search = '5';
$matches = explode('-',array_values(preg_grep('/[-]/', $myarr))[0]);
if(in_array($search, $myarr)|| in_array($search,range($matches[0],$matches[1]))){
echo "found";
}else{
echo "not found";
}
我通过-
爆炸,然后用它创建范围,并用in_数组检查它
下面的函数返回$haystack
中的$needle
索引,如果未找到此类元素,则返回-1
function find(array $haystack, $needle) {
for ($i = 0; $i < count($haystack); $i++) {
$parts = explode("-", $haystack[$i]);
if (count($parts) == 2) {
if ($needle >= $parts[0] && $needle <= $parts[1]) {
return $i;
}
}
else if ($needle == $parts[0]) {
return $i;
}
}
return -1;
}
下面的函数返回$haystack
或-1
中$needle
的索引(如果未找到此类元素)
function find(array $haystack, $needle) {
for ($i = 0; $i < count($haystack); $i++) {
$parts = explode("-", $haystack[$i]);
if (count($parts) == 2) {
if ($needle >= $parts[0] && $needle <= $parts[1]) {
return $i;
}
}
else if ($needle == $parts[0]) {
return $i;
}
}
return -1;
}
我知道答案已经被接受了,但这里是我的方法,使用foreach
()
对于foreach:
<?php
$value = 3;
foreach($myarr as $myval){
list($start, $end) = strpos($myval, '-') ? explode('-', $myval) : [$myval, $myval];
if($value >= $start && $value <= $end){
echo $value . ' found between "' . $start . '" & "' . $end . '" !' . "\n";
}
}
我知道答案已经被接受,但这里是我使用foreach的方法w/和w/o
()
对于foreach:
<?php
$value = 3;
foreach($myarr as $myval){
list($start, $end) = strpos($myval, '-') ? explode('-', $myval) : [$myval, $myval];
if($value >= $start && $value <= $end){
echo $value . ' found between "' . $start . '" & "' . $end . '" !' . "\n";
}
}
我正在等待id有人发布单行或双线函数..我看到的只是分解和比较使用一个foreach
并在每次迭代中检查两个变量并将结果保存为布尔值…?!@deceze我没有understand@deceze你能解释一下吗?@MASforeach($v为myarr){if(/*检查$search2*/)$search2Found=true;如果(/*检查$search5*/)$Search5Find=true;}如果($search2Found&$search5已找到)…
我正在等待有人发布单行或双线函数..我看到的只是分解和比较,然后在每次迭代中使用一个foreach
并检查两个变量,并将结果保存为布尔值…?!@deceze我没有understand@deceze你能解释一下吗?@MASforeach($v为myarr){if(/*检查$search2*/)$search2Found=true;如果(/*检查$search5*/)$search5Found=true;}如果($search2Found&&$search5Found)…
我知道这个方法。我需要魔法函数。这不是我想要的魔法函数。这是我能写的最好的。我喜欢@sahilgulati的代码。我知道这个方法。我需要魔法函数。这不是我想要的魔法函数。这是我能写的最好的。我喜欢@sahilgulati的code@BilalAhmed谢谢……:)为什么global
?一次处理一个项目。为什么在回调中需要整个数组?我将其设置为全局,以便在有人要搜索值1
时处理正常情况say@axiac请检查第二个foreach。您应该使用use
来传递外部变量,而不是global
ode>elseif
分支没有什么意义;如果你能做一个简单的$number==$search
,为什么要在数组中使用昂贵的?这是如何解决这两个搜索条件的?@BilalAhmed谢谢…:)为什么global
?一次处理一个项目。为什么在回调中需要整个数组?我将其设置为全局,以便在有人要搜索值1
时处理正常情况say@axiac请检查第二个foreach。您应该使用use
来传递外部变量,而不是global
ode>elseif