The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Page 3 -
PHP-General - Array creation [was: Need help]
Page 3 - 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 17th, 2013, 11:27 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Quote: | 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)) == 0 ) {
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.
|

January 17th, 2013, 11:28 AM
|
|
|
|
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.
|

January 17th, 2013, 11:32 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 297
  
Time spent in forums: 3 Days 8 h 45 m 39 sec
Reputation Power: 5
|
|
|
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.
|

January 17th, 2013, 11:42 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
Time spent in forums: 3 h 22 m 59 sec
Reputation 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
|

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

January 17th, 2013, 12:14 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 16
Time spent in forums: 3 h 22 m 59 sec
Reputation Power: 0
|
|
|
Thanks you very much. You helped me that is exactly what I was looking for.
|
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
|
|
|
|
|