排序多维数组并保持兄弟姐妹

时间:2013-02-27 01:49:55

标签: php arrays multidimensional-array

我如何在type首先url,然后是doc然后page来订购以下数组?

$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'
    )
);

维持密钥关联并不重要。

5 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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" );

答案 2 :(得分: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");

答案 3 :(得分: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。如果这些语句都不为true且element1是doc,则返回true,因为element2是doc或page。最后,如果element1是一个页面,则总是返回false,因为element2是doc或page。

答案 4 :(得分: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>';