按字母顺序排列对象数组

时间:2014-09-16 00:50:47

标签: php sorting smarty

如何按displayName对PHP对象数组进行排序,并在智能模板中按字母顺序输出?

if(Tools::getValue("action") == "contacts") {
    $config = _PS_CLASS_DIR_ . 'hybridauth/config.php';
    require_once(_PS_CLASS_DIR_ .  "hybridauth/Hybrid/Auth.php" );

    $serviceProvider = Tools::getValue("service");
    $hybridauth = new Hybrid_Auth( $config );
    $service = $hybridauth->authenticate( $serviceProvider );
    $contacts = $service->getUserContacts();

    if (is_array($contacts)){
        self::$smarty->assign('contacts', $contacts);
        self::$smarty->display(_PS_THEME_DIR_.'addressbook.tpl');
    }
    $service->logout(); 
    die();
}

我输出的列表如下:

<div id="choose-contacts" class="container">
    <div class="info">
        <ul class="thumbnails">
        {foreach from=$contacts key=k item=contact}
            <li class="span3">
                <input type="checkbox" id="{$contact->identifier}">
                <div class="thumbnail">
                    <div class="row-fluid team">
                        {if isset($contact->photoURL)}
                        <img src="{$contact->photoURL}" onclick="toggle('{$contact->identifier}')"/>
                        {else}
                        <img src="{$img_dir}avatar.jpg" onclick="toggle('{$contact->identifier}')" />
                        {/if}
                    </div>
                    <div class="row-fluid">
                        <h3>
                            {if ($contact->displayName !== '')}
                                {$contact->displayName}
                            {else}
                                {$contact->email}
                            {/if}
                        </h3>
                    </div>
                </div>
            </li>
        {/foreach}
        </ul>
    </div>
</div>

但现在我想用字母索引修改它..

<div class="container">
    <ul class="alpha-nav">
        <li><a href="#A">A</a></li>
        <li><a href="#B">B</a></li>
        ...
    </ul>

    <div class="content-holder">
        <div id="A">A</div>
        <ul class="content thumbnails">
            {foreach from=$contacts key=k item=contact}
            <li>
            {if ($contact->displayName !== '')}
                {$contact->displayName}
            {else}
                {$contact->email}
            {/if}
            </li>
        </ul>
        <div id="B">B</div>
        <ul class="content thumbnails">
            {foreach from=$contacts key=k item=contact}
            <li>
            {if ($contact->displayName !== '')}
                {$contact->displayName}
            {else}
                {$contact->email}
            {/if}
            </li>
        </ul>
        ...
    </div>
</div>

<script>
    var scrollSpeed = 500,
        ease = 'easeOutCubic'

    $('ul.alpha-nav a').click(function() {

        var id = $(this).attr('href');
        $('.content-holder').css('overflow', 'visible');
        var offset = $(id).offset().top - 40;
        $('.content-holder').css('overflow', 'hidden');
        $('.content-holder').animate({ scrollTop: offset},scrollSpeed,ease);
        return false;
    });
</script>

1 个答案:

答案 0 :(得分:1)

首先使用usort对联系人进行排序,然后使用关联数组将displayName的第一个字母分隔为存储桶

$contacts = $service->getUserContacts();
if (is_array($contacts)){
    usort($contacts,function($a,$b) { return strcmp($a->displayName,$b->displayName);});
    $contactsByLetter = array();
    foreach($contacts as $contact) {
        $contactsByLetter[$contact->displayName[0]][] = $contact->displayName;
    }
    self::$smarty->assign('contactsByLetter', $contactsByLetter);
    self::$smarty->assign('contacts', $contacts);
    self::$smarty->display(_PS_THEME_DIR_.'addressbook.tpl');
}

然后遍历smarty中的关联数组

{foreach from=$contactsByLetter key=letter item=contacts}
    <div id="{ letter }">{ letter }</div>
    <ul class="content thumbnails">
        {foreach from=contacts key=k item=contact}
        <li>
        {if ($contact->displayName !== '')}
            {$contact->displayName}
        {else}
            {$contact->email}
        {/if}
        </li>
        {/foreach}
    </ul>
{/foreach}