1

我怎样才能按顺序排列以下数组typeurl然后docpage

$array = array(
    0 => array(
        'id' => '14',
        'position' => '0',
        'type' => 'related-url'
    ),
    1 => array(
        'id' => '2367',
        'position' => '0',
        'type' => 'related-doc'
    ),
    2 => array(
        'id' => '99',
        'position' => '0',
        'type' => 'related-page'
    ),
    4 => array(
        'id' => '180',
        'position' => '2',
        'type' => 'related-doc'
    ),
    5 => array(
        'id' => '10',
        'position' => '3',
        'type' => 'related-doc'
    )
);

结果将是

$array = array(
    0 => array(
        'id' => '14',
        'position' => '0',
        'type' => 'related-url'
    ),
    1 => array(
        'id' => '2367',
        'position' => '0',
        'type' => 'related-doc'
    ),
    4 => array(
        'id' => '180',
        'position' => '2',
        'type' => 'related-doc'
    ),
    5 => array(
        'id' => '10',
        'position' => '3',
        'type' => 'related-doc'
    ),
    2 => array(
        'id' => '99',
        'position' => '0',
        'type' => 'related-page'
    )
);

保持键关联并不重要。

4

5 回答 5

1

好吧,这很丑陋,但它有效)

$doc = array();
$url = array();
$page = array();

foreach($array as $v)
    switch($v['type']){
        case 'related-url': $url[] = $v;break;
        case 'related-doc': $doc[] = $v;break;
        case 'related-page': $page[] = $v;break;
    }

$array = array_merge($url, $doc, $page);
于 2013-02-27T02:03:32.123 回答
1

给你

function cmp($a, $b) {

    $v1 = $a['type'];
    $v2 = $b['type'];   
    $v1 = str_replace("related-", "", $v1);
    $v2 = str_replace("related-", "", $v2); 
    $firstChar = abs(ord(substr($v1,0,1)) - ord("u"));
    $firstChar2 = abs(ord(substr($v2,0,1)) - ord("u"));

    return $firstChar<=$firstChar2?-1:1; 

}

usort ( $array, "cmp" );
于 2013-02-27T02:13:21.277 回答
1
function cmp($a, $b) {

    $sortOrder = array(
        "related-url"   => 1,
        "related-doc"   => 2,
        "related-page"  => 3
    );

    return $sortOrder[$a['type']] - $sortOrder[$b['type']];
}

usort($array, "cmp");
于 2013-02-27T17:37:07.523 回答
0

为此使用 usort,这是一个简单的示例。我很确定这个逻辑有效,但毫无疑问有更好的方法来做到这一点。

usort($your_array, function ($elem1, $elem2) {
  if($elem1['type'] == 'related-url')
    return true;

  elseif($elem2['type'] == 'related-url' )
    return false;

  elseif($elem1['type'] == 'related-doc')
    return true;

  else
    return false;
});

如果 element1 是 url 它总是返回 true,如果它不是并且 element2 是 url 它总是 false。如果这些语句都不为真并且 element1 是文档,则返回 true,因为 element2 是文档或页面。最后,如果 element1 是页面,则始终返回 false,因为 element2 是文档或页面。

于 2013-02-27T02:09:38.907 回答
0

这应该工作

$urls = $docs = $pages = array();
foreach ($array as $sub) {
    switch($sub['type']) {
        case 'related-url':
            $urls[] = $sub;
        break;
        case 'related-doc':
            $docs[] = $sub;
        break;
        case 'related-page':
            $pages[] = $sub;
        break;
   }
}
$result = array_merge($urls, $docs, $pages);
echo '<pre>', var_dump($result), '</pre>';
于 2013-02-27T02:09:56.520 回答