wantime
asked on
how to sort 2 dimensiona array in php
hi there,
i have put a lot array into one array called $mPeople, now i would like to sort $mPeople by 'smoker', any Tips?
Codes related:
//$Human is array which i got from web service.
$mPeople= array();
foreach($Human as $id=>$people) {
$everyone = array('id'=>$people->mId,' name'=>$pe ople->mNam e, 'hobby'=>$people->mHobby, 'smoker'=>$people->isSmoke r);
array_push($mPeople,$every one);
}
thanks,
wantime
i have put a lot array into one array called $mPeople, now i would like to sort $mPeople by 'smoker', any Tips?
Codes related:
//$Human is array which i got from web service.
$mPeople= array();
foreach($Human as $id=>$people) {
$everyone = array('id'=>$people->mId,'
array_push($mPeople,$every
}
thanks,
wantime
Sorry, that link is duff, should be
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3212-Using-PHP-classes-to-group-data-in-memory.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_3212-Using-PHP-classes-to-group-data-in-memory.html
ASKER
i have tried following codes, but it doesn|t work, i just get error
Warning: usort() [function.usort]: Invalid comparison function...
public function cmp($a, $b) {
if (($a[3] == $b[3]) ) {
return 0;
} else if ($a[3]){
return 1;
} else {
return -1;
}
}
usort($mPeople, "cmp");
Warning: usort() [function.usort]: Invalid comparison function...
public function cmp($a, $b) {
if (($a[3] == $b[3]) ) {
return 0;
} else if ($a[3]){
return 1;
} else {
return -1;
}
}
usort($mPeople, "cmp");
ASKER
@bportlock
thanks. i have just readed your comment..
what i want to say is that the $a['smoker'] is a boolean type.
thanks. i have just readed your comment..
what i want to say is that the $a['smoker'] is a boolean type.
In that case change the comparison function
function compareArray( $a, $b ) {
if ( $a['smoker'] && !$b['smoker'] )
return -1;
else
if ( !$a['smoker'] && $b['smoker'] )
return 1;
return 0;
}
function compareArray( $a, $b ) {
if ( $a['smoker'] && !$b['smoker'] )
return -1;
else
if ( !$a['smoker'] && $b['smoker'] )
return 1;
return 0;
}
ASKER
i used
uasort( $mPeople, 'compareArray');
but got the error:
uasort() [function.uasort]: Invalid comparison function...
uasort( $mPeople, 'compareArray');
but got the error:
uasort() [function.uasort]: Invalid comparison function...
Check for either a mis-spelling of "compareArray" and that the function is within the scope of the uasort. I usually ensure that functions are declared higher up the program than their point of use.
Also are you doing this in "ordinary code" or in a class? The syntax is different within a class if the comparison function is a member function. It would be
uasort( $mPeople, array( $this, 'compareArray') );
If all else fails, post the code here
Also are you doing this in "ordinary code" or in a class? The syntax is different within a class if the comparison function is a member function. It would be
uasort( $mPeople, array( $this, 'compareArray') );
If all else fails, post the code here
ASKER
the method uasort( $mPeople, 'compareArray'); cause error
"uasort() [function.uasort]: Invalid comparison function...", and when i use
method "uasort( $mPeople, array( $this, 'compareArray') );", i get errorinfo "Application Error" on the website.
i use the method in one class, the structure looks like following:
"uasort() [function.uasort]: Invalid comparison function...", and when i use
method "uasort( $mPeople, array( $this, 'compareArray') );", i get errorinfo "Application Error" on the website.
i use the method in one class, the structure looks like following:
<?php
class Model_Person
{
public function compareArray( $a, $b ) {
if ( $a['smoker'] && !$b['smoker'] )
return -1;
else
if ( !$a['smoker'] && $b['smoker'] )
return 1;
return 0;
}
//comment (removed)
public function getInfoFromWS() {
$mPeople = array();
... //get $Human from Web Service
foreach($Human as $id=>$people) {
$everyone = array('id'=>$people->mId,'name'=>$people->mName, 'hobby'=>$people->mHobby, 'smoker'=>$people->isSmoker);
array_push($mPeople,$everyone);
}
//uasort($mPeople, 'compareArray');
uasort( $mPeople, array( $this, 'compareArray') );
}
return $mPeople;
}
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks bportlock.
I object to this being deleted on behalf of bportlock who has obviously put in a lot of effort to solve this. If there is some code that shouldn't be public it should be edited out by a mod but the rest of the question should remain and bportlock should get the points.
:-O
Very kind of you snarfles. I'm speechless and that doesn't happen often.
ASKER
if one can delete the comment in both codes, that would be nice.
ASKER
who is the mod and how can i contact with him? i have no right to modify the comment.
Hi Vee Mod
Thanks for your comment. I could have sworn that I clicked an object button and that I filled in a reason for doing so. That being said I've never had a reason to object before so I guess I could have stuffed it up.
At any rate, this isn't my question, nor am I the person who answered it. I was just monitoring it to learn from it and thought that bportlock deserved the points.
Do you now want me to click 'request attention' or is it fine from now on?
Thanks
Snarfles
Thanks for your comment. I could have sworn that I clicked an object button and that I filled in a reason for doing so. That being said I've never had a reason to object before so I guess I could have stuffed it up.
At any rate, this isn't my question, nor am I the person who answered it. I was just monitoring it to learn from it and thought that bportlock deserved the points.
Do you now want me to click 'request attention' or is it fine from now on?
Thanks
Snarfles
ASKER
hi all,
i have already click the 'request attention' and wrote the reason there. I just wait for mod to edit out the codes.
thanks,
wantime
i have already click the 'request attention' and wrote the reason there. I just wait for mod to edit out the codes.
thanks,
wantime
ASKER
thanks you for your work!
btw, i never refuse to award points to the Experts, i just wanted to correct the mistake at first.
btw, i never refuse to award points to the Experts, i just wanted to correct the mistake at first.
http://www.php.net/uasort
Your comparison function would look something like
function compareArray( $a, $b ) {
if ( $a['smoker'] < $b['smoker'] )
return -1;
else
if ( $a['smoker'] > $b['smoker'] )
return 1;
return 0;
}
and then you would call it like so
uasort( $myArrayToSort, 'compareArray');
(all the above UNTESTED)
For future work you may want to consider this https://www.experts-exchange.com/A_316-Simplify-your-coding---string-handling-in-HTML-&-PHP.html