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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Compare and Match Arrays

    I was introduced and learned about comparing arrays about a week ago, using the array_diff function.

    But is there a way to compare 2 arrays and have it return only the matches?

    Would you use the in_array function to do that? Would that return all matches? Or just a True or False?

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Internet Explorer

    I'm not sure if thats what I'm looking for, but I amm still too green to truly understand all of the array functions out there.

    Basically, I'm looking to search two arrays, and foreach match that exists, I want to be able to echo out that number.

    I had though using in_array was the correct approach. I would think the below code should, but its not finding a match.
    PHP Code:
    // Set the Variables
    $capitals '11,12';
    $player_states '11,12';
            
    //Explode them into arrays
    $capitals explode(',',$capitals);
    $player_states explode(',',$player_states);                                        
                        
    if(
    in_array($capitals$player_states)) {
        echo 
    'We have a match';

    Last edited by ptmuldoon; 03-02-2007 at 10:08 PM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by ptmuldoon View Post
    I'm not sure if thats what I'm looking for essentially if I have, but I'm still to green to truly understand all of the array functions out there.

    Basically, I'm looking to search two arrays, and foreach match that exists, I want to be able to echo out that number.

    I had though using in_array was the correct approach.
    PHP Code:
    foreach($capitals as $k)
    {
        if(
    in_array($k$player_states))
            echo 
    $k;

    I think I misread that. I think the above code is what you're looking for.
    Last edited by Inigoesdr; 03-02-2007 at 10:15 PM. Reason: Updated with your variables.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    but I amm still too green to truly understand all of the array functions out there.
    Sit down, create a new PHP file. And start trying them out.

    There's quite some useful ones out there.

    Know your tools.


  •  

    Posting Permissions

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