如何在多级数组中搜索文本(PHP)
我想搜索像下面这样的数组来查找pair2.php.net - 搜索域的最有效方法是什么?带有 stripos 的循环 foreach 语句?
Array
(
[0] => Array
(
[host] => php.net
[type] => MX
[pri] => 5
[target] => pair2.php.net
[class] => IN
[ttl] => 6765
)
[1] => Array
(
[host] => php.net
[type] => A
[ip] => 64.246.30.37
[class] => IN
[ttl] => 8125
)
)
这是第一条评论建议中的我的工作代码 - 不同的方式会更有效吗?
$search = 'secureserver.net';
$x='no!';
foreach($result AS $array1)
{
foreach($array1 AS $array2)
{
if(stripos($array2,$search))
{
$x='yes!';
}
}
}
echo $x;
I would like to search through an array like the one below looking for pair2.php.net - what would be the most efficient way to search through the domain? A looping foreach statement with stripos?
Array
(
[0] => Array
(
[host] => php.net
[type] => MX
[pri] => 5
[target] => pair2.php.net
[class] => IN
[ttl] => 6765
)
[1] => Array
(
[host] => php.net
[type] => A
[ip] => 64.246.30.37
[class] => IN
[ttl] => 8125
)
)
Here's my working code from the first comment's suggestion - would a different way be more efficient?
$search = 'secureserver.net';
$x='no!';
foreach($result AS $array1)
{
foreach($array1 AS $array2)
{
if(stripos($array2,$search))
{
$x='yes!';
}
}
}
echo $x;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用单个 for 循环并使用 php 中的 'in_array' 构造来查找数组中的值:-
in_array( 'pair2.php.net', $sub_array );
you could use a single for-loop and use the 'in_array' construct in php to look for the value in an array :-
in_array( 'pair2.php.net', $sub_array );