开发者

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

开发者 https://www.devze.com 2023-02-28 21:14 出处:网络
Hi i have a multidimensional associative array: $array= array( \'Book1\' => array(\'http://www.google.com\', \'45\' ),

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'.

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

B开发者_运维问答ut 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. While this does work, when you have a very large haystack array, performance suffers. I would recommend using isset() for testing if a needle exists in the haystack array.

I modified the function to return an array of the found results to remove any performance hits from outputing to stdout. 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:

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; 
            } 
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消