0

を使用してusort()、整数値も含む文字列を並べ替えることができますか?

たとえば、電子メール アドレス (およびその他のデータ) を含むオブジェクトの配列を見てみましょう。

$invitees = Array(
    [0] => Array(
        'email' => 'test11@testing.com'
    ),
    [1] => Array(
        'email' => 'test2@testing.com'
    ),
    [2] => Array(
        'email' => 'test1@testing.com'
    )
);

次のコードを使用すると、配列要素が単純な文字列として比較されます -

/** Sort the data (if the sort key is defined) */
if(!empty($_REQUEST['orderby'])) :
    usort($emails, array(&$this, '_order_callback'));
endif;

function _order_callback($item_a, $item_b){

    /** Grab 'orderby', which must have been set for this function to be called */
    $orderby = $_REQUEST['orderby'];

    /** If no 'order' is not set, default to ASC */
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'ASC';

    $result = strcmp($item_a[$orderby], $item_b[$orderby]);

    return (strtoupper($order) === 'ASC') ? $result : -$result; //Send final sort direction to usort

}

結果は次の順序で配信されます -

[0] - 'test11@testing.com'
[2] - 'test1@testing.com'
[1] - 'test2@testing.com'

私はこの順序を望んでいますが -

[2] - 'test1@testing.com'
[1] - 'test2@testing.com'
[0] - 'test11@testing.com'

これは可能usort()ですか?ありがとう。


編集

natsort()(以下のコメント/回答のおかげで)の存在を知ったので、これを見つけて試すことができました-

$result = ($item_a[$orderby] > $item_b[$orderby] ? 1 : ($item_a[$orderby] < $item_b[$orderby] ? -1 : 0));

その比較を_order_callback()関数 ( if $orderby === email) に追加しましたが、近いですが、 order でソートされ11, 12, 13, 14, 1, 2, 3, 4ます。

4

3 に答える 3

2

strnatcmp()を使用して、usort() コールバックで比較を行います

$email1 = new StdClass;
$email1->email = 'test11@testing.com';
$email2 = new StdClass;
$email2->email = 'test1@testing.com';
$email3 = new StdClass;
$email3->email = 'test2@testing.com';
$email4 = new StdClass;
$email4->email = 'test12@testing.com';
$email5 = new StdClass;
$email5->email = 'test21@testing.com';
$email6 = new StdClass;
$email6->email = 'test3@testing.com';

$invitees = array(
    $email1,
    $email2,
    $email3,
    $email4,
    $email5,
    $email6,
);


usort($invitees, '_order_callback');

function _order_callback($item_a, $item_b){
    return strnatcmp($item_a->email, $item_b->email);
}

var_dump($invitees);
于 2013-08-08T17:50:10.793 に答える
1

Mark Ba​​ker が言うように、natsortあなたが探しているものです。

$emails = array(
    'test2@testing.com',
    'test11@testing.com',
    'test1@testing.com'
);

natsort( $emails );
于 2013-08-08T16:01:29.780 に答える
0

@MarkBaker、@MajorCaiger、@hdvianna のおかげで、私が使用している最終注文のコールバック関数を以下に示します。

この関数は、多次元配列からの複数の並べ替え基準を考慮に入れます。

/** Sort the data (if the sort key is defined) */
if(!empty($_REQUEST['orderby'])) :
    usort($invitees[$status], array(&$this, '_order_callback'));
endif;

/**
 * Callback function to order event invitees
 *
function _order_callback($item_a, $item_b){

    /** Grab 'orderby', which must have been set for this function to be called */
    $orderby = $_REQUEST['orderby'];

    /** If no 'order' is not set, default to ASC */
    $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'ASC';

    switch($orderby) :

        case 'ID' : // ID is unique, so just sort by ID
            $result = strnatcmp($item_a[$orderby], $item_b[$orderby]);
            break;

        case 'email' :
            $result = strnatcasecmp($item_a[$orderby], $item_b[$orderby]);
            if($result === 0) :
                $result = strcmp($item_a['first_name'], $item_b['first_name']);
            endif;
            if($result === 0) :
                $result = strcmp($item_a['surname'], $item_b['surname']);
            endif;
            break;

        case 'name' :
            $result = strcmp($item_a['first_name'], $item_b['first_name']); // Explicitly declare 'first_name' here as $orderby is actuualy 'name', which is a constructed field for display
            if($result === 0) :
                $result = strcmp($item_a['surname'], $item_b['surname']);
            endif;
            if($result === 0) :
                $result = strnatcasecmp($item_a['email'], $item_b['email']);
            endif;
            break;

        default : // 'custom' and 'town'
            $result = strcmp($item_a[$orderby], $item_b[$orderby]);
            break;

    endswitch;

    return (strtoupper($order) === 'ASC') ? $result : -$result; //Send final sort direction to usort

}
于 2013-08-09T09:26:52.497 に答える