Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts

    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
    Banned
    Join Date
    Mar 2013
    Posts
    139
    Thanks
    0
    Thanked 9 Times in 9 Posts
    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.

  • Users who have thanked knightCoder for this post:

    shaunthomson (03-30-2013)

  • #3
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    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 title

    foreach($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;
        }


  • Users who have thanked patryk for this post:

    shaunthomson (03-30-2013)

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    shaunthomson (03-30-2013)

  • #5
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    now that's one lucky post haha

  • #6
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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.
    Last edited by shaunthomson; 03-30-2013 at 07:01 AM.


  •  

    Posting Permissions

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