Sort a multidimensional array by integer inside of a string value which is two levels down



PHP Snippet 1:

$data = [
    [
        'certificate_name' => 'track.site',
        'domains' => 'track.site',
        'expiry_date' => [
            'date' => '2018-09-25',
            'time' => '10:11:58',
            'count' => 22,
        ]
    ],
    [
        'certificate_name' => 'stats.com',
        'domains' => 'stats.com',
        'expiry_date' => [
            'date' => '2018-09-24',
            'time' => '10:11:58',
            'count' => 43,
        ]
    ]
];

function compare_by_expiry_date_count($a, $b) {
    return $a["expiry_date"]['count'] > $b["expiry_date"]['count'];
}


usort($data, "compare_by_expiry_date_count");

var_dump($data);

PHP Snippet 2:

<?php
// this $b-$a is for desc, for asc try $a-$b
function sorting_by_count($a, $b)
{
    return trim($b['expiry_date']['count'],'()') - trim($a['expiry_date']['count'],'()');
}
$array = [
    [
        'certificate_name' => 'track.site',
        'domains' => 'track.site',
        'expiry_date' => [
            'date' => '2018-09-25',
            'time' => '10:11:58',
            'count' => '(22)',
        ]
    ],
    [
        'certificate_name' => 'stats.com',
        'domains' => 'stats.com',
        'expiry_date' => [
            'date' => '2018-09-24',
            'time' => '10:11:58',
            'count' => '(43)',
        ]
    ]
];
usort($array, 'sorting_by_count');
print_r($array);
?>

PHP Snippet 3:

foreach ($array as $row) {
    $counts[] = (int) trim($row['expiry_date']['count'], '()');
}
array_multisort($counts, $array);
var_export($array);