The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Array creation [was: Need help]
Discuss Array creation [was: Need help] in the PHP Development forum on Dev Shed. Array creation [was: Need help] PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 16th, 2013, 03:30 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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.
|

January 16th, 2013, 04:56 PM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
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.";
}
|

January 16th, 2013, 05:01 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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,..)
|

January 17th, 2013, 07:37 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
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.
|

January 17th, 2013, 09:25 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
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.
|

January 17th, 2013, 09:30 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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';
}
|

January 17th, 2013, 09:35 AM
|
|
|
|
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

January 17th, 2013, 09:45 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
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?
|

January 17th, 2013, 09:47 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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.
|

January 17th, 2013, 10:09 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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?
|

January 17th, 2013, 10:20 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
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?
|

January 17th, 2013, 10:29 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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
|

January 17th, 2013, 10:34 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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 )
|

January 17th, 2013, 10:35 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 295
  
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.
|

January 17th, 2013, 10:39 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
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?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|