如何通过多层次阵列寻找文本搜索(PHP) [英] How to search through multi level arrays looking for text (PHP)

查看:94
本文介绍了如何通过多层次阵列寻找文本搜索(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过类似下面寻找pair2.php.net阵列搜索 - 这将是通过域搜索的最有效的方法是什么?与stripos函数一个循环foreach语句?

 阵列

    [0] =>排列
        (
            [主持人] = GT; php.net
            [类型] = GT; MX
            (PRI)=>五
            [目标] => pair2.php.net
            [类] =>在
            [TTL] => 6765
        )    [1] =>排列
        (
            [主持人] = GT; php.net
            [类型] = GT;一个
            [IP] => 64.246.30.37
            [类] =>在
            [TTL] => 8125
        ))

下面是从第一个评论的建议,我的工作code - 会用不同的方式更有效?

  $搜索='secureserver.net';$ X ='不!';的foreach($结果作为数组1 $)
{
    的foreach($数组1 AS数组2 $)
    {
        如果(stripos函数($数组2,$搜索))
        {
            $ X ='是的!';
        }
    }}
回声$ X;


解决方案

您可以使用一个for循环,并使用in_array构建在PHP中查找该值在数组中: -

in_array('pair2.php.net',$ sub_array);

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;

解决方案

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

这篇关于如何通过多层次阵列寻找文本搜索(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆