PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2013, 03:30 PM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation Power: 0
PHP-General - 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.

Reply With Quote
  #2  
Old January 16th, 2013, 04:56 PM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 295 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
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.";


Reply With Quote
  #3  
Old January 16th, 2013, 05:01 PM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation 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,..)

Reply With Quote
  #4  
Old January 17th, 2013, 07:37 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,809 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 18 h 4 m 15 sec
Reputation Power: 6112
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.

Reply With Quote
  #5  
Old January 17th, 2013, 09:25 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 295 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
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 09:42 AM.

Reply With Quote
  #6  
Old January 17th, 2013, 09:30 AM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation 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';
}

Reply With Quote
  #7  
Old January 17th, 2013, 09:35 AM
gw1500se gw1500se is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,880 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 2 Days 5 h 26 m 40 sec
Reputation Power: 581
I think you want array_diff.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #8  
Old January 17th, 2013, 09:45 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 295 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
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?

Reply With Quote
  #9  
Old January 17th, 2013, 09:47 AM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation Power: 0
array_diff doesn't work. I have to check elements in both arrays if they are same return true if not false.

Reply With Quote
  #10  
Old January 17th, 2013, 10:09 AM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation 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?

Reply With Quote
  #11  
Old January 17th, 2013, 10:20 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 295 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
Will a particular array always be equal to or longer than the other? Or will each one be back and forth?

Reply With Quote
  #12  
Old January 17th, 2013, 10:29 AM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation 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

Reply With Quote
  #13  
Old January 17th, 2013, 10:34 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,809 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 18 h 4 m 15 sec
Reputation Power: 6112
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 )

Reply With Quote
  #14  
Old January 17th, 2013, 10:35 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 295 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 3 Days 8 h 37 m 29 sec
Reputation Power: 5
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.

Reply With Quote
  #15  
Old January 17th, 2013, 10:39 AM
Dzeno Dzeno is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 16 Dzeno User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 22 m 59 sec
Reputation 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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Array creation [was: Need help]

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap