PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > the usort 's puzzle
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
the usort 's puzzle

Réponse
 
LinkBack Outils de la discussion
Vieux 30/09/2007, 04h40   #1
youngord@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut the usort 's puzzle

<?php
$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
echo $x[0];
}

usort($mix,'com');

?>

i think the $x[0] result is A,
but the final $x[0] result is BC.
why???

  Réponse avec citation
Vieux 30/09/2007, 14h16   #2
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the usort 's puzzle

youngord@gmail.com wrote:
> <?php
> $mix=array(
> array("A",10),
> array("B",5),
> array("C",100)
> );
> function com($x,$y){
> echo $x[0];
> }
>
> usort($mix,'com');
>
> ?>
>
> i think the $x[0] result is A,
> but the final $x[0] result is BC.
> why???
>


Your com() function doesn't return a value, as required by usort(). So
results are completely unpredictable.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
  Réponse avec citation
Vieux 30/09/2007, 14h44   #3
J.O. Aho
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the usort 's puzzle

youngord@gmail.com wrote:
> <?php
> $mix=array(
> array("A",10),
> array("B",5),
> array("C",100)
> );
> function com($x,$y){
> echo $x[0];
> }
>
> usort($mix,'com');
>
> ?>
>
> i think the $x[0] result is A,
> but the final $x[0] result is BC.
> why???


first round of usort does:

com(array("B",5),array("A",10))

which leads to that

$x=array("B",5)
$y=array("A",10)

then you echo

$x[0]

which has the value

B

the function returns nothing, which is equal to false (this moves a after b in
the mix array)

second round of usort does:

com(array("C",100),array("B",5))

which leads to that

$y=array("C",100)
$y=array("B",5)

then you echo

$x[0]

which has the value

C

the function returns nothing, which is equal to false (which moves c before b
in the mix array)


Which leads to that you geat an output of

BC

The $mix array looks now like

array(3) {
[0]=>
array(2) {
[0]=>
string(1) "C"
[1]=>
int(100)
}
[1]=>
array(2) {
[0]=>
string(1) "B"
[1]=>
int(5)
}
[2]=>
array(2) {
[0]=>
string(1) "A"
[1]=>
int(10)
}
}


I guess you really wanted

<?php
$mix=array(
array("A",10),
array("B",5),
array("C",100)
);
function com($x,$y){
return ($x[1]<$y[1])?false:true;
}

usort($mix,'com');

//To allow you to see the sorted array
var_dump($mix);
?>

Which gives you

array(3) {
[0]=>
array(2) {
[0]=>
string(1) "B"
[1]=>
int(5)
}
[1]=>
array(2) {
[0]=>
string(1) "A"
[1]=>
int(10)
}
[2]=>
array(2) {
[0]=>
string(1) "C"
[1]=>
int(100)
}
}

Kind of BAC.

--

//Aho
  Réponse avec citation
Vieux 01/10/2007, 02h08   #4
youngord@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: the usort 's puzzle

On Sep 30, 9:44 pm, "J.O. Aho" <u...@example.net> wrote:
> young...@gmail.com wrote:
> > <?php
> > $mix=array(
> > array("A",10),
> > array("B",5),
> > array("C",100)
> > );
> > function com($x,$y){
> > echo $x[0];
> > }

>
> > usort($mix,'com');

>
> > ?>

>
> > i think the $x[0] result is A,
> > but the final $x[0] result is BC.
> > why???

>
> first round of usort does:
>
> com(array("B",5),array("A",10))
>
> which leads to that
>
> $x=array("B",5)
> $y=array("A",10)
>
> then you echo
>
> $x[0]
>
> which has the value
>
> B
>
> the function returns nothing, which is equal to false (this moves a after b in
> the mix array)
>
> second round of usort does:
>
> com(array("C",100),array("B",5))
>
> which leads to that
>
> $y=array("C",100)
> $y=array("B",5)
>
> then you echo
>
> $x[0]
>
> which has the value
>
> C
>
> the function returns nothing, which is equal to false (which moves c before b
> in the mix array)
>
> Which leads to that you geat an output of
>
> BC
>
> The $mix array looks now like
>
> array(3) {
> [0]=>
> array(2) {
> [0]=>
> string(1) "C"
> [1]=>
> int(100)
> }
> [1]=>
> array(2) {
> [0]=>
> string(1) "B"
> [1]=>
> int(5)
> }
> [2]=>
> array(2) {
> [0]=>
> string(1) "A"
> [1]=>
> int(10)
> }
>
> }
>
> I guess you really wanted
>
> <?php
> $mix=array(
> array("A",10),
> array("B",5),
> array("C",100)
> );
> function com($x,$y){
> return ($x[1]<$y[1])?false:true;
>
> }
>
> usort($mix,'com');
>
> //To allow you to see the sorted array
> var_dump($mix);
> ?>
>
> Which gives you
>
> array(3) {
> [0]=>
> array(2) {
> [0]=>
> string(1) "B"
> [1]=>
> int(5)
> }
> [1]=>
> array(2) {
> [0]=>
> string(1) "A"
> [1]=>
> int(10)
> }
> [2]=>
> array(2) {
> [0]=>
> string(1) "C"
> [1]=>
> int(100)
> }
>
> }
>
> Kind of BAC.
>
> --
>
> //Aho


thanks very much. i understand clearly.^_^

  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 05h23.


É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,12492 seconds with 12 queries