PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Compare each distinct pair of the entires in the 1D associativearrays
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Compare each distinct pair of the entires in the 1D associativearrays

Réponse
 
LinkBack Outils de la discussion
Vieux 16/02/2008, 05h49   #1
duzhidian@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Compare each distinct pair of the entires in the 1D associativearrays

Dear All:

For the indexed 1D array, I can easily use the following statements:


for ($index2 = 0; $index2 < $size; $index2++){
for ($index1 = $index2+1; $index1 < $size; $index1++)

to compare each distinct pair of the entires in the 1D array (the
upper triangle of the n*n square).

For the associative arrays, how can I do that?

Thanks.

Du
  Réponse avec citation
Vieux 17/02/2008, 00h30   #2
Jeremy@thebunnyshed.co.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

foreach
  Réponse avec citation
Vieux 17/02/2008, 01h17   #3
duzhidian@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

On Feb 16, 7:30 pm, Jer...@thebunnyshed.co.uk wrote:
> foreach


I know foreach. But using foreach how to do it?
  Réponse avec citation
Vieux 17/02/2008, 01h53   #4
petersprc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

You can get random access to keys using the array returned by
array_keys().

If it's the case that the arrays don't have the same keys or you can
have duplicate keys in an array (unlike your numerical array example),
then you can keep track of pairs that have already been seen, e.g.:

if (isset($seen[$k][$j])) continue;

$seen[$j][$k] = true;
$seen[$k][$j] = true;

On Feb 16, 12:49 am, "duzhid...@gmail.com" <duzhid...@gmail.com>
wrote:
> Dear All:
>
> For the indexed 1D array, I can easily use the following statements:
>
> for ($index2 = 0; $index2 < $size; $index2++){
> for ($index1 = $index2+1; $index1 < $size; $index1++)
>
> to compare each distinct pair of the entires in the 1D array (the
> upper triangle of the n*n square).
>
> For the associative arrays, how can I do that?
>
> Thanks.
>
> Du


  Réponse avec citation
Vieux 17/02/2008, 02h00   #5
duzhidian@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

Thanks. But if considering the efficiency of the PHP program, I think
foreach would be much faster. Right?



On Feb 16, 8:53 pm, petersprc <peters...@gmail.com> wrote:
> You can get random access to keys using the array returned by
> array_keys().
>
> If it's the case that the arrays don't have the same keys or you can
> have duplicate keys in an array (unlike your numerical array example),
> then you can keep track of pairs that have already been seen, e.g.:
>
> if (isset($seen[$k][$j])) continue;
>
> $seen[$j][$k] = true;
> $seen[$k][$j] = true;
>
> On Feb 16, 12:49 am, "duzhid...@gmail.com" <duzhid...@gmail.com>
> wrote:
>
> > Dear All:

>
> > For the indexed 1D array, I can easily use the following statements:

>
> > for ($index2 = 0; $index2 < $size; $index2++){
> > for ($index1 = $index2+1; $index1 < $size; $index1++)

>
> > to compare each distinct pair of the entires in the 1D array (the
> > upper triangle of the n*n square).

>
> > For the associative arrays, how can I do that?

>
> > Thanks.

>
> > Du


  Réponse avec citation
Vieux 17/02/2008, 02h13   #6
duzhidian@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

Dear petersprc:

If we use array_keys() to extract all keys, we just use the
traditional indexed array of keys to keep track of all pair of keys
from this 1D array, why bothering us to declare a new 2D array,
$seen[$j][$k]?




On Feb 16, 8:53 pm, petersprc <peters...@gmail.com> wrote:
> You can get random access to keys using the array returned by
> array_keys().
>
> If it's the case that the arrays don't have the same keys or you can
> have duplicate keys in an array (unlike your numerical array example),
> then you can keep track of pairs that have already been seen, e.g.:
>
> if (isset($seen[$k][$j])) continue;
>
> $seen[$j][$k] = true;
> $seen[$k][$j] = true;
>
> On Feb 16, 12:49 am, "duzhid...@gmail.com" <duzhid...@gmail.com>
> wrote:
>
> > Dear All:

>
> > For the indexed 1D array, I can easily use the following statements:

>
> > for ($index2 = 0; $index2 < $size; $index2++){
> > for ($index1 = $index2+1; $index1 < $size; $index1++)

>
> > to compare each distinct pair of the entires in the 1D array (the
> > upper triangle of the n*n square).

>
> > For the associative arrays, how can I do that?

>
> > Thanks.

>
> > Du


  Réponse avec citation
Vieux 17/02/2008, 02h35   #7
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

duzhidian@gmail.com wrote:
> Dear All:
>
> For the indexed 1D array, I can easily use the following statements:
>
>
> for ($index2 = 0; $index2 < $size; $index2++){
> for ($index1 = $index2+1; $index1 < $size; $index1++)
>
> to compare each distinct pair of the entires in the 1D array (the
> upper triangle of the n*n square).
>
> For the associative arrays, how can I do that?
>
> Thanks.
>
> Du
>


Du,

Something like:

$match = true;
if (count($array1) != count($array2)) {
$match = false;
else {
foreach ($array1 as $key->$value) {
if (!isset($array2[$key]) || $array2[$key] <> $value) {
$match = false;
break;
}
}
}



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Vieux 17/02/2008, 02h54   #8
petersprc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Compare each distinct pair of the entires in the 1D associativearrays

Yeah, in the second case array_keys isn't applicable

$seen is needed if you're checking unique key pairs and there are no
restrictions on the array contents.

On Feb 16, 9:13 pm, "duzhid...@gmail.com" <duzhid...@gmail.com> wrote:
> Dear petersprc:
>
> If we use array_keys() to extract all keys, we just use the
> traditional indexed array of keys to keep track of all pair of keys
> from this 1D array, why bothering us to declare a new 2D array,
> $seen[$j][$k]?
>
> On Feb 16, 8:53 pm, petersprc <peters...@gmail.com> wrote:
>
> > You can get random access to keys using the array returned by
> > array_keys().

>
> > If it's the case that the arrays don't have the same keys or you can
> > have duplicate keys in an array (unlike your numerical array example),
> > then you can keep track of pairs that have already been seen, e.g.:

>
> > if (isset($seen[$k][$j])) continue;

>
> > $seen[$j][$k] = true;
> > $seen[$k][$j] = true;

>
> > On Feb 16, 12:49 am, "duzhid...@gmail.com" <duzhid...@gmail.com>
> > wrote:

>
> > > Dear All:

>
> > > For the indexed 1D array, I can easily use the following statements:

>
> > > for ($index2 = 0; $index2 < $size; $index2++){
> > > for ($index1 = $index2+1; $index1 < $size; $index1++)

>
> > > to compare each distinct pair of the entires in the 1D array (the
> > > upper triangle of the n*n square).

>
> > > For the associative arrays, how can I do that?

>
> > > Thanks.

>
> > > Du


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15257 seconds with 16 queries