#16
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    And as far as those 2 arrays, do you want that to provide a TRUE or FALSE about them matching? I can write a true since the items they both hold DO match, but I can also write a false, since one array holds values the second does not, making them NOT matching.


    EDIT: As far as creating arrays, they can be created a million ways. Is this info pulled form a database, file, set items, ...?
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    is it possible to create arrays differently, probably that is not a good way to create them based on the information I have?
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    No I got assignment and I have to solve it. the key here is to create arrays so if else statement could work.
    even if I create them like this if else doenst work again
    $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();

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

    if($array1==$array2){
    echo 'true';
    }
    else{
    echo 'false';
    }
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    I dun suppose you'd be willing to describe and lay out how/what you did to solve your issue? This will help uture individuals search the web for a similar issue. Congrats n have fun!
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    No I haven't solved yet :/ it works only if you have the same number of elements in arrays
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Then make it do a count on arrays, and have it proccess the one with a lesser count.
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    this is the assignement
    Write a program to compare 2 arrays of objects of the same type. For example array1 is array
    of Phones objects: Phone1{name: Iphone, year: 2006}, Phone2{name:Motorola, year:2008},Phone3, Phone4;
    array2 : Phone5, Phone6, If the arrays are equal expected result is true, otherwise false

    according to this I have to create arrays and see are they equal
  14. #23
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    This is the code you posted:

    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(); 

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

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

    There is one problem with it: You use $array1 every time, never filling $array2. Once I fix that, and clean up the formatting, I get:
    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(
    $array1==$array2){
      echo 
    'true';
    } else {
      echo 
    'false';

    That produces "false" for me. That is working by all definitions of the word. What, exactly, is the problem you're having?
    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.
  16. #24
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Originally Posted by Dzeno
    No I got assignment and I have to solve it. the key here is to create arrays so if else statement could work.
    even if I create them like this if else doenst work again
    $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();

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

    if($array1==$array2){
    echo 'true';
    }
    else{
    echo 'false';
    }
    You are initializing the $array2 but you never add elements to it. Instead you add to $array1 again. If you run this code then the output is 'false', which is correct because the araays are not equal.

    If you run this code:

    Code:
    <?php
    
    $array1 = array();
    
    $array1[] = array('name' => 'Iphone', 'year' => '2008');
    $array1[] = array('name' => 'Motorola', 'year' => '2006');
    
    $array2 = array();
    
    $array2[] = array('name' => 'Iphone', 'year' => '2008');
    $array2[] = array('name' => 'Motorola', 'year' => '2006');
    
    if($array1==$array2){
    echo 'true';
    }
    else{
    echo 'false';
    }
    
    ?>
    Then you will get the output 'true'.

    If you change the code to :

    Code:
    <?php
    
    $array1 = array();
    
    $array1[] = array('name' => 'Iphone', 'year' => '2008');
    $array1[] = array('name' => 'Motorola', 'year' => '2006');
    
    $array2 = array();
    
    $array2[] = array('name' => 'Iphone', 'year' => '2008');
    $array2[] = array('name' => 'Motorola', 'year' => '2007');
    
    if($array1==$array2){
    echo 'true';
    }
    else{
    echo 'false';
    }
    
    ?>
    Then the output is false.

    To conclude: the == operator works for arrays.
  18. #25
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Ok. Array 1 has 6 phones, and array 2 has 4 phones. If all phones in array 2 r identical to array 1, do you want this to respond TRUE? Or, because array 1 has phones array 2 doesn't, do you want this to respond FALSE?
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Yes you are right I didnt make a good copy here I have fixed that. It still doesnt work because it only check are the number of elements inside arrays are the same? it doesnt check the content
  22. #27
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    It still doesnt work because it only check are the number of elements inside arrays are the same? it doesnt check the content
    This is wrong. Aurum84 showed you code which proves that it's false, since Aurum84's code contained two arrays of 2 elements each, with only a single data element being different, and the code returned "false."

    Again: What, exactly, is the problem. Show code which you believe returns the wrong output. You can't just keep saying the english sentence "it doesn't work." We can't see "it," nor can we read your mind. Show code. Tell us what that code produces. Tell us why you believe that output is wrong.

    And read the new user guide, specifically the part about asking good questions.
    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.
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    for me this assignment is a bit confusing. First array has 4 phones, 2nd array has 2 phones. If first array contains elements from second array would return true if not false.
    I think they are looking for that in the assignment
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    So, if $array1 has 20 phones, and $array2 has 5 phones, and all phones in $array2 match those in array 1, this should be true? My goal is just wondering if count should matter, or just similar items to be compared.
  28. #30
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    I think count doesnt matter here only similar items to be comapred. Array one can have 50elements array2 can have only one element. If the element from array 2 is contained in array one will return true.

IMN logo majestic logo threadwatch logo seochat tools logo