1

I have an array :

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

and am trying to write a function where you can search and return an array with the $keys and $values intact. So if someone where to search "'Book1' , 'Book2' then they would get back an array :

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

This:

$bookArray = array()
$bookDetailsarray = array();

$needles  = array('book1' , 'book2' ); 

   foreach ($needles as $needle) {

    foreach ($array as $key => $value) 
        { 

            if ($key == $needle) 
                { 
                array_push($BookArray, $key);
                array_push($bookDetailsarray, $value);
                array_push($bookArray, $bookDetailsarray);                                                          
                }             
        }
     }


  } 

This works, but on each iteration of the foreach it keeps adding the $bookDetailsaray to the $value. So it returns:

Book1 => [0]'Book1details' 
Book2 => [0]'Book1details' [1]'Book2details'
Book3 => [0]'Book1details' [1]'Book2details' [2] 'Book3details'

and so on.

I want to be able to do:

array_push($BookArray, $key=>$value);

but obviously thats not possible. Any ideas? Even if its just what array function I need .

2 Answers 2

2

A slightly more efficient method to do your search using basic PHP functions rather than looping:

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

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

$searchResults = array_intersect_key($searchArray,array_flip($needles));

var_dump($searchResults);

But note that it is case-sensitive

EDIT

If you wanted a case-insensitive search, you could use array_intersect_ukey() instead, using a custom comparison to ignore the case of the keys.

function key_compare_func($key1, $key2) {
    $key1 = strtoupper($key1);
    $key2 = strtoupper($key2);

    if ($key1 == $key2)
        return 0;
    else if ($key1) > $key2)
        return 1;
    else
        return -1;
}

$searchResults = array_intersect_ukey($searchArray, array_flip($needles), 'key_compare_func');

EDIT 2

Using strcasecmp() can make the user-defined key comparison a lot simpler too.

function key_compare_func($key1, $key2) {
    return strcasecmp($key1,$key2);
}
0
0

Use this logic instead (although, I am not 100% what you want the last line to do?)

$BookArray[]=$key;
$bookDetailsarray[]=$value;
$bookArray[]=$bookDetailsarray;

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.