0

Let's say I have an object called ACCOUNT like:

class ACCOUNT
{
private $contact;
public function getContact();
}

and another object CONTACT:

class CONTACT
{
   private $lastName;
   public function getLastName()
   {
      return $this->lastName;
   }
}

Then I create an array of these objects $accountArray = ACCOUNT::get(); How can I sort this array alphabetically by something like $account->getContact()->getLastName();?

What I've tried:

class Account
{
    private $contact;
    public function getContact();

    public static function cmp($a,$b)
    {
       $al = strtolower($a->getContact()->getLastName());
       $bl = strtolower($b->getContact()->getLastName());
       if ($al == $bl) {
       return 0;
       }
       return ($al > $bl) ? +1 : -1;
    }
    public static function sortByLastName($accountArray)
    {
       usort($moACOUNTArray, array('ACCOUNT', 'cmp'));
    }
}

But I get this error:

 Call to undefined method ACCOUNT::getContact()
5
  • 4
    php.net/manual/en/function.usort.php Commented Feb 8, 2017 at 19:58
  • How much have you tried yourself? It probably is a good idea to just do research and learn how to do this. Here is a start, the magic link Commented Feb 8, 2017 at 20:02
  • Sorry, I've tried usort, but there's just something I'm missing about how to set it up. Commented Feb 8, 2017 at 20:06
  • With only $contact variable, how do you expect being able to access the last name? If $contact is also a Class, please provide it. Commented Feb 8, 2017 at 20:07
  • The method must be static if it's intended to be called using the class-name alone. Or else an object will have to be created using which it could be invoked. Reference manual Commented Feb 8, 2017 at 20:09

1 Answer 1

1

You can use usort function of PHP, like mentioned in comments. There is self-explaining example:

Try this code:

<?php

class ACCOUNT
{
    private $contact;
    public function getContact()
    {
        return $this->contact;
    }

    public function setContact($v)
    {
        $this->contact = $v;
    }
}

class CONTACT
{
    private $lastName;

    public function getLastName()
    {
        return $this->lastName;
    }

    public function setLastName($v)
    {
        $this->lastName = $v;
    }
}

//create data for testing
$c1 = new CONTACT;
$c1->setLastName('aaaa');
$a1 = new ACCOUNT;
$a1->setContact($c1);

$c2 = new CONTACT;
$c2->setLastName('zzz');
$a2 = new ACCOUNT;
$a2->setContact($c2);

$c3 = new CONTACT;
$c3->setLastName('ggg');
$a3 = new ACCOUNT;
$a3->setContact($c3);

$array = array($a1, $a2, $a3);


//making sort
function cmp($a, $b)
{
    return strcmp($a->getContact()->getLastName(), $b->getContact()->getLastName());
}

usort($array, "cmp");

//showing sort result
echo '<pre>';
print_r($array);

Result is:

Array
(
    [0] => ACCOUNT Object
        (
            [contact:ACCOUNT:private] => CONTACT Object
                (
                    [lastName:CONTACT:private] => aaaa
                )

        )

    [1] => ACCOUNT Object
        (
            [contact:ACCOUNT:private] => CONTACT Object
                (
                    [lastName:CONTACT:private] => ggg
                )

        )

    [2] => ACCOUNT Object
        (
            [contact:ACCOUNT:private] => CONTACT Object
                (
                    [lastName:CONTACT:private] => zzz
                )

        )

)
Sign up to request clarification or add additional context in comments.

3 Comments

Whenever I try this I still get this error Call to undefined method moACCOUNT::getContact() in /controllers/accountController.php on line 37
In code you've posted in question, in class ACCOUNT, function getContact() has no body. That is the problem. Check my ACCOUNT class. And try my code.
Sorry, this was a brain lapse on my part. I was supposed to be calling ACCOUNT->getPrimaryClinet()->getContact()->getLastName();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.