array_search() not working correctly?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi everyone. I am having a problem with array_search(). My php script
gets a list of books from the database, and then compares them with a
list of books which have been obtained from a textarea in a form, and
formatted into single books, each one stored in a non-associative

I have a function that compares the books in the $user_books array, to
those in the array derived from the books that are in the database.

If a book exists in the database, then it needs to be added to the user
table, for this I need the book id. If it doesn't the user will be
given a list of such books, displaying the names of the books that
aren't in the database.

If I created the list my making an array manually of both user books,
and books that are in the database, everything works fine, however, as
things are, array_search() won't return the key, if the book title has
a number in it.

I have posted my script below. I know it looks big and messy, but I
just wanted to know if anyone could spot any errors that may be leading
to the problem I am experiencing. I generally try to keep the script
simple, and then once it's doing what I want, I then work on optimizing

$input = (isset($_POST['input'])) ? $_POST['input'] : '';

function validateBooks(&$booklist) {
    global $db;
    if ($booklist) {
        $query =
        "SELECT `book_name` AS name, `book_id` AS bid FROM
        $dbbooks_res = $db->sql_query($query);
        $newbooklist = array();
        $dbbooks = array();
        while ($book = $db->sql_fetchrow($dbbooks_res)) {
            $book_name = $book['name'];
            $dbbooks[$book['bid']] = $book_name;
        foreach ($booklist as $book_name) {
            $key = array_search("$book_name", $dbbooks);
            echo "key:$key<br />";
            if ($key) {
                $newbooklist['valid'][] = $key;
            else {
                $newbooklist['invalid'][] = $book_name;
        echo "Add<br />";
        echo "<pre>",print_r($newbooklist['valid']),"</pre>";
        echo "Don't add<br />";
        echo "<pre>",print_r($newbooklist['invalid']),"</pre>";

function process($input='') {
    if ($input) {
        $input = preg_replace("/([\n\r]|.+) Special Books \][\n\r]/s",
"", $input);
        $input = preg_replace("/\[ .+/s", "", $input);
        $input = preg_replace("/[\r\n]+/", "\n", $input);
        $input_lines=preg_split("/\n/", $input);
        $sizeof_input_lines = sizeof($input_lines);
        $book_list = array();
        for ($i=0; $i<$sizeof_input_lines; $i++) {
            if (strlen($input_lines[$i])>1) {
                $book_list[] = $input_lines[$i];
        $books = sizeof($book_list);
        $not_added = validateBooks($book_list);

echo "<center><subtitle>Import Books</subtitle></center><p>&nbsp;</p>";
echo "<table>";
if ($input) {

echo "<tr><td>".showImportBox()."</td></tr>";
echo "</table>";

function showImportBox() {
    global $module_name;
    echo <<<EOT
<form method="post" action="index.php?name=$module_name">
<center><textarea name="input" cols="80"
rows="30"></textarea></center><br />
<center><input type="submit" name="submit_import" value="Import
<input type="hidden" name="do" value="Import My Books">
<input type="hidden" name="Import My Books" value="1">


Re: array_search() not working correctly?

Can you give us examples as to what titles work and what titles don't?
It only doesn't work when there's a number in the book title? What does
array_search return?

a good debug idea would be to print the value of $book_name each time
you are about to run an array_search().

Daz wrote:
Quoted text here. Click to load it

Re: array_search() not working correctly?

maven22 wrote:
Quoted text here. Click to load it

I don't believe it! I have figured out why it's not working, and man,
do I feel silly?

The books aren't in the database. It just happened to be that not a
single one of these 6 books were in there and they happened to be the
only 6 in my query containing numbers...

Thanks for your input, and sorry for wasting any of your time.

Sincerest regards.


Site Timeline