简体   繁体   English

PHP 通过键搜索多维关联数组并返回键=>值

[英]PHP Search multidimensional associative array by key and return key => value

Hi i have a multidimensional associative array:嗨,我有一个多维关联数组:

   $array= array(
            'Book1' => array('http://www.google.com', '45' ),
            'Book2' => array('http://www.yahoo.com', '46', )
)

I need to be able to search $array on 'BookX' and then return the contents of 'BookX'.我需要能够在“BookX”上搜索 $array,然后返回“BookX”的内容。

Ive tried:我试过了:

  function array_searcher($needles, $array) 
    { 
   foreach ($needles as $needle) {

    foreach ($array as $key ) 
        { 

            if ($key == $needle) 
                { 
                echo $key; 
                } 

        }
        }
  } 

with the search随着搜索

$needles  = array('Book1' , 'Book2' ); 

But this doesnt return anything但这不会返回任何东西

I might be misunderstanding, but this just sounds like the accessor.我可能会误解,但这听起来像是访问器。 If not, could you clarify?如果不是,你能澄清一下吗?

$array= array(
    'Book1' => array('http://www.google.com', '45' ),
    'Book2' => array('http://www.yahoo.com', '46', )
);

echo $array['Book1'];

EDIT: I did misunderstand your goal.编辑:我确实误解了你的目标。 I do have a comment on doing the two foreach loops.我确实对执行两个 foreach 循环有意见。 While this does work, when you have a very large haystack array, performance suffers.虽然这确实有效,但当您有一个非常大的 haystack 数组时,性能会受到影响。 I would recommend using isset() for testing if a needle exists in the haystack array.我建议使用isset()来测试 haystack 数组中是否存在针。

I modified the function to return an array of the found results to remove any performance hits from outputing to stdout.我修改了 function 以返回找到的结果数组,以消除输出到标准输出的任何性能损失。 I ran the following performance test and while it might not do the same search over and over, it points out the inefficiency of doing two foreach loops when your array(s) are large:我运行了以下性能测试,虽然它可能不会一遍又一遍地进行相同的搜索,但它指出了当您的数组很大时执行两个 foreach 循环的效率低下:

function array_searcher($needles, $array) {

    $result = array();

    foreach ($needles as $needle) {
        foreach ($array as $key => $value) {
            if ($key == $needle) {
                $result[$key] = $value;
            }
        }
    }

    return $result;
}

function array_searcher2($needles, $array) {

    $result = array();

    foreach ($needles as $needle) {
        if (isset($array[$needle])) {
            $result[$needle] = $array[$needle];
        }
    }

    return $result;
}

// generate large haystack array
$array = array();
for($i = 1; $i < 10000; $i++){
    $array['Book'.$i] = array('http://www.google.com', $i+20);
}

$needles = array('Book1', 'Book2');

$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    array_searcher($needles, $array);
}
echo (microtime(true) - $start)."\n";
// 14.2093660831

$start = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    array_searcher2($needles, $array);
}
echo (microtime(true) - $start)."\n";
// 0.00858187675476

If you want to search using the keys, you should access it as a key => value pair.如果要使用键进行搜索,则应将其作为键 => 值对访问。 If you don't it only retrieves the value.如果你不这样做,它只会检索值。

function array_searcher($needles, $array) 
{ 
    foreach ($needles as $needle) 
    {
        foreach ($array as $key => $value) 
        { 
            if ($key == $needle) 
            { 
                echo $key; 
            } 
        }
    }
}

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

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