0

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

2
  • Solved by using : foreach ($array as $key => $value ) Commented Apr 27, 2011 at 13:58
  • An answer to this question without needing to build nested loops is posted in response to stackoverflow.com/questions/5806245/… Commented Apr 27, 2011 at 15:37

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks sorry I didn't explain , but the $needle array could be any size and have any values in it. As is sometimes the case, sitting here and retyping the code it then made sense, I need ' foreach ($array as $key ) ' to be 'foreach ($array as $key => $value ) '
@Bob, edited after I reread your question and understood it properly.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.