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 .