Page 3 of 3 First 123
  • Jump to page:
    #31
  1. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    If first array contains elements from second array would return true if not false.
    This is the first time you've said this. It took 20+ posts for the actual question to come to light. You've been saying "compare the arrays" and "equal" this whole time.

    You also said array_diff wouldn't work, which further mislead us, since that's the answer.

    PHP Code:
    $array1 = array();  
    $array1[] = array('name' => 'Iphone''year' => '2008');  
    $array1[] = array('name' => 'Motorola''year' => '2006'); 
    $array1[] = array('name' => 'Sony''year' => '2006'); 
    $array1[] = array('name' => 'Nokia''year' => '2006'); 

    $array2 = array();  
    $array2[] = array('name' => 'Iphone''year' => '2008');  
    $array2[] = array('name' => 'Motorola''year' => '2006'); 

    if ( 
    count(array_diff($array2$array1)) == ) {
      echo 
    'true'
    } else { 
      echo 
    'false'

    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  2. #32
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    With that description of the problem, you would go to an implementation of a Set. And regarding this as a homework assignment, I would not be surprised if that is what your teachers pursue. Look up the specifics of a Set and tell me if you agree.
  4. #33
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Ok. Then retrieve a count on each array. Make it run the lesser/equal array as its counter.
    Last edited by Triple_Nothing; January 17th, 2013 at 11:41 AM.
  6. #34
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Aurum you probably you are right.

    ManiacDan solution you have posted will display true even if the elements form array 2 are not contained in array 1
  8. #35
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Ah right, I forgot array_diff doesn't work in multi-dimensional arrays.

    Use loops:
    PHP Code:
    $array1 = array();   
    $array1[] = array('name' => 'Iphone''year' => '2008');   
    $array1[] = array('name' => 'Motorola''year' => '2006');  
    $array1[] = array('name' => 'Sony''year' => '2006');  
    $array1[] = array('name' => 'Nokia''year' => '2006');  

    $array2 = array();   
    $array2[] = array('name' => 'Iphone''year' => '2008');   
    $array2[] = array('name' => 'Motorola''year' => '2006');  


    $equal true;
    foreach ( 
    $array2 as $val ) {
      if ( !
    in_array($val$array1 ) ) {
        
    $equal false;
        break;
      }
    }

    if ( 
    $equal ) { 
      echo 
    'true';  
    } else {  
      echo 
    'false';  

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #36
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Thanks you very much. You helped me that is exactly what I was looking for.
Page 3 of 3 First 123
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo