简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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