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 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array comparison

    I have an array (call it $temp) and an array of arrays (call it $results).

    I'd like to loop through the $results and see if the first element in $temp matches the first element of each array in $results.

    Can anyone help me with how to do this?

    Many thanks
    Rich

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Welcome here !

    Something like
    PHP Code:
    foreach($results as $var){
       if (
    $var[0]!=$temp[0]){
          echo (
    'Problem: mismatch found for ');
          
    print_r($var);     
       }

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the welcome

    I haven't tried that yet, but it looks self explanatory and spot on for what I want, cheers

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're welcome.

    I just ran this
    PHP Code:
    $status='ok';
    $temp= array('1','2','3');
    $results= array(array('1','3'),
                    array(
    '1','4'),
                    array(
    '2','10'));  
    foreach(
    $results as $var){
       if (
    $var[0]!=$temp[0]){
          
    $status='not ok';
          echo (
    '<br />Problem: mismatch found for ');
          
    print_r($var);
       }
    }
    echo (
    '<br />' $status); 
    and it worked.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html


  •  

    Posting Permissions

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