-2
     foreach($domainCheckResults as $domainCheckResult)
    {
        switch($domainCheckResult->status)
        {
            case Transip_DomainService::AVAILABILITY_INYOURACCOUNT:
                $result .= "<p style='color:red;'>".$domainCheckResult->domainName."</p>";
            break;

            case Transip_DomainService::AVAILABILITY_UNAVAILABLE:
                $result .= "<p style='color:red;'>".$domainCheckResult->domainName."</p>";
            break;

            case Transip_DomainService::AVAILABILITY_FREE:
                $result .= "<p style='color:#1aff1a;'>".$domainCheckResult->domainName."   "."<a href='ticket.php'><img src='img/next.png' alt='house'                                                      width='20' height='20' align='right' title='domein aanvragen?'></a>"."</p>";
            break;

            case Transip_DomainService::AVAILABILITY_NOTFREE:
                $result .= "<p style='color:#ff9933;'>".$domainCheckResult->domainName."   "."<a href='contactform.php'><img src='img/65.png' alt='house'                                          width='20' height='20' align='right' title='domein laten verhuizen?'></a>"."</p>";
            break;
        }
    }

So I have 4 possible results for domain availability. I have a array with 20 domains and when I get the results I get them in the sorting of my array. But How can I sort is on availability, so the once that are free all at the top and the once that are not free for whatever reason down? What can I use? I used the sort() tag but that won`t help.

1 Answers1

4

You'll need to define the order in some way and then use usort() appropriately.

For example:

<?php

interface Transip_DomainService
{
    public const AVAILABILITY_INYOURACCOUNT = 'inyouraccount';
    public const AVAILABILITY_UNAVAILABLE   = 'unavailable';
    public const AVAILABILITY_FREE          = 'free';
    public const AVAILABILITY_NOTFREE       = 'notfree';
}

// define required order
$priority = [
    Transip_DomainService::AVAILABILITY_FREE          => 0,
    Transip_DomainService::AVAILABILITY_NOTFREE       => 1,
    Transip_DomainService::AVAILABILITY_UNAVAILABLE   => 2,
    Transip_DomainService::AVAILABILITY_INYOURACCOUNT => 3,
];

$domainCheckResults = [
    (object) ['domainName' => 'a', 'status' => Transip_DomainService::AVAILABILITY_NOTFREE],
    (object) ['domainName' => 'b', 'status' => Transip_DomainService::AVAILABILITY_UNAVAILABLE],
    (object) ['domainName' => 'c', 'status' => Transip_DomainService::AVAILABILITY_INYOURACCOUNT],
    (object) ['domainName' => 'd', 'status' => Transip_DomainService::AVAILABILITY_FREE],
    (object) ['domainName' => 'e', 'status' => Transip_DomainService::AVAILABILITY_INYOURACCOUNT],
    (object) ['domainName' => 'f', 'status' => Transip_DomainService::AVAILABILITY_FREE],
];

// apply sort using order lookup
usort($domainCheckResults, function ($a, $b) use ($priority) {
    return $priority[$a->status] <=> $priority[$b->status];
});

print_r($domainCheckResults);

demo: https://3v4l.org/j24rM

Yoshi
  • 54,081
  • 14
  • 89
  • 103