Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0

    Array creation [was: Need help]


    I am starting to learn PHP and I do not know how to create an array in this situation and solve problem.

    I have to compare two arrays. array1 is array
    of Phone objects: Phone1{name: Iphone, year: 2010}, Phone2{name:Samsung, year:2011}, Phone3, Phone4;
    array2 : Phone5, Phone6, Phone7,.. If the arrays are equal result is true, if not false.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    PHP Code:
    // Set to be matching/TRUE
    $array1 = array("March","April","May");
    $array2 = array("March","April","May");
    if(
    $array1 == $array2) {
      echo 
    "True: They ARE equal.";
    } else {
      echo 
    "False: They are NOT equal.";

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Yes but how to create arrays based on the information I have. I am having trouble with that.
    (array1 is array
    of Phone objects: Phone1{name: Iphone, year: 2010}, Phone2{name:Samsung, year:2011}, Phone3, Phone4;
    array2 : Phone5, Phone6, Phone7,..)
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Arrays can hold arrays:

    PHP Code:
    $phones = array();

    $phones[] = array('name' => 'iphone''year' => '2010');
    $phones[] = array('name' => 'motorola''year' => '1980'); 
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    I guess in the end, how do you intend on using/referencing the content of an array?
    Last edited by Triple_Nothing; January 17th, 2013 at 10:42 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    I have created array like this.. But if else statement doesnt work here. is there a function that will check arrays and then use if else statement or I have to do it on different way?

    $phones = array();
    $phones[] = array('name' => 'Iphone', 'year' => '2008','name' => 'Motorola', 'year' => '2008', 'Nokia' => 'Toyota', 'year' => '2008','name' => 'Sony', 'year' => '2008',);

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

    if ($phones == $phones){
    echo 'true';
    }
    else{
    echo 'false';
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,600
    Rep Power
    595
    I think you want array_diff.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    With your example of arrays, I would assume you expect your if/then to return false. Am I correct? What IS happening that isn't as desired?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    array_diff doesn't work. I have to check elements in both arrays if they are same return true if not false.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Yes exactly what is happening is not as desired. If I use if else statement only if I have the same number of elements in each array would work and if they are same will return true if not false. But what I can use if I dont have the same number of elements? Is there any function to check?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    Will a particular array always be equal to or longer than the other? Or will each one be back and forth?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    It doesnt matter the content of array. I have to check both arrays and display are they equal or not that is the aim of this assignment. In this case one array is longer. that is why if else statement doesnt work. it works only if arrays have the same number of elements
  24. #13
  25. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    You're creating your arrays wrong. Look how your code differs from mine. You have a single array with the key "year" and "name" being repeated 3 times each. That won't work. Use print_r to print an array to see if it even contains the data you think it should. Yours will not.

    The new user guide in my signature contains useful information for you.

    The == operator is all you need once your arrays are made correctly.

    if ( $a == $b )
    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.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    721
    Rep Power
    7
    And a note... You can't build your arrays as you are. They must be adjusted as follows:

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

    $phones2 = array();
    $phones2[] = array(array('name' => 'Iphone''year' => '2008'), array('name' => 'Motorola''year' => '2008')); 
    The way you current have them built keeps over-writing the past assigned value, so you will only end up with the final name/year assigned in each array.
  28. #15
  29. 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?
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo