Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Searching for matches in multidimensional array

1. ## Searching for matches in multidimensional array

Gidday

I'm been playing around with this for a few hours now, and haven't got it working yet - I'm wondering if there is a standard way to do this?

I have a multidimensional array - here's a cut down version containing just two items:

Code:
`\$mainArray = array( array("myID"=>"103", "Author"=>"John Doe", "Title"=>"Doe's Way"), array("myID"=>"54542", "Author"=>"Mary Contrary", "Title"=>"The Fork in the Road"));`
I need to return the mainArray index on matches for both Author and Title.

Thanks for your time and help.

2. This looks like a homework exercise. This should help get you started. It looks for an author in the 2D array and if found outputs the row number it is in. Note, rows start at 0.
PHP Code:
``` <?php\$mainArray = array(    array("myID" => "103", "Author" => "John Doe", "Title" => "Doe's Way"),    array("myID" => "54542", "Author" => "Mary Contrary", "Title" => "The Fork in the Road"));\$author = "Mary Contrary";\$rowNum = 0;\$foundAuthor = false;foreach(\$mainArray as \$row){    foreach(\$row as \$value){        if(\$value == \$author){            \$idx = \$rowNum;            \$foundAuthor = true;        }    }    \$rowNum++;}if(\$foundAuthor){    echo \$author.'is in row '.\$idx;}else{    echo \$author.' is not in the array';}?> ```
You should be able to add the code to look for the title as well.

3. ## Users who have thanked knightCoder for this post:

shaunthomson (03-30-2013)

4. this might work
PHP Code:
``` \$mainArray = array( array("myID"=>"103", "Author"=>"John Doe", "Title"=>"Doe's Way"), array("myID"=>"54542", "Author"=>"Mary Contrary", "Title"=>"The Fork in the Road"));\$author = 'John Doe'; //searched author\$title = 'Doe\'s Way'; //searched titleforeach(\$mainArray as \$index => \$value){    if(\$value['Author'] == \$author && \$value['Title'] == \$title){        //uncomment for full record        //echo \$index . '-' . \$value['myID'] . '-' . \$value['Author'] . '-' . \$value['Title'] . '<br />';        echo \$index;    }}  ```

5. ## Users who have thanked patryk for this post:

shaunthomson (03-30-2013)

6. The easiest way I can think of is to use a filter:
PHP Code:
``` function findAuthorAndTitle(array \$a, \$sAuthor, \$sTitle){    \$fn = function(array \$a) use (\$sAuthor, \$sTitle)    {        \$bResult = true;        if (isset(\$a['Author'], \$a['Title']))        {            \$bResult &= \$bResult && (strcasecmp(\$a['Author'], \$sAuthor) == 0);            \$bResult &= \$bResult && (strcasecmp(\$a['Title'], \$sTitle) == 0);        }        else        {            \$bResult = false;        }        return \$bResult;    };    return array_filter(\$a, \$fn);}  ```
Then just call that with:
PHP Code:
``` \$filtered = findAuthorAndTitle(\$mainArray, 'Author name here', 'title here');  ```
It can be modified to deal with having no author name or no title as well, but I didn't add that here.
For just the keys, grab array_keys off of the \$filtered result.

7. ## Users who have thanked Fou-Lu for this post:

shaunthomson (03-30-2013)

8. now that's one lucky post haha

9. Yes -I was spoilt - thank you guys.

I took bits of each of your replies, and did this:

Code:
```\$originalArray = array( array("myID"=>"103", "Author"=>"John Doe", "Title"=>"Doe's Way"), array("myID"=>"54542", "Author"=>"Mary Contrary", "Title"=>"The Fork in the Road"));

\$resultArray = array();

while( \$row = mysql_fetch_assoc(\$result))
{

for ( \$i=0; \$i < sizeof(\$originalArray); \$i++ )
{
if( \$originalArray[\$i]['Author'] == \$row['A'] && \$originalArray[\$i]['Title'] == \$row['T'] )
{
\$testString.="Author: ". \$row['A']. ", Title: ". \$row['T'] .", Index: ".\$i;
\$resultArray[] = \$originalArray[\$i];
array_splice(\$originalArray, \$i, 1);
break 1;
}
}

}

echo \$testString;```

It gets the index on matches against \$originalArray from a database query. I found that using array_splice to get rid of each matched item from \$originalArray resulted in a 10x faster process than leaving the matched item in the array (did this test on an array of a few thousand items).

Thanks guys - I would have been working on this for another four hours if it wasn't for your posts.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•