2016-07-29 23 views
2

は私が並べ替えしようとしていることを次の配列している:私は目的の出力を取得しようとしている多次元配列をソート問題

[0]=>Array 
{ 
    [date_test] => 2016-01-01 
    [last_name] => Smith 
} 
[1]=>Array 
{ 
    [date_test] => 2015-01-01 
    [last_name] => Davis 
} 
[2]=>Array 
{ 
    [date_test] => 2015-01-01 
    [last_name] => Smith 
} 

:ここ

Davis 2015-01-01 
Smith 2015-01-01 
Smith 2016-01-01 

は、私は何でありますコードとして使用:

foreach ($total_records as &$test) { 
    $test[] = $this->sortArray($test,array('patient_last_name','issued_at')); 
} 

sortArray($data, $field) 
{ 
    $field = (array) $field; 
    uasort($data, function($a, $b) use($field) { 
    $retval = 0; 
    foreach($field as $fieldname) 
    { 
     if($retval == 0) $retval = strnatcmp($a[$fieldname], $b[$fieldname]); 
    } 
    return $retval; 
}); 
return $data; 
} 

希望の出力を得るにはどのようなアイデア?

+2

あなたはALL.'usort($ arrが、機能($、$ b)は{returnのときにそのループを必要としません。 $ a ['date_test'] <=> $ b ['date_test']}) ' –

+1

@MarcBあなたのコード出力は、OPの望む出力とは少し異なります。 –

答えて

1

$sort_vals = array(); 
foreach($mylist as $k=>$v) { 
    $sort_vals['date_test'][$k] = $v['date_test']; 
    $sort_vals['last_name'][$k] = $v['last_name']; 
} 

たevent_type DESCでソートし、

array_multisort($sort_vals['last_name'], SORT_DESC, $sort_vals['date_test'], SORT_ASC,$mylist); 
+0

それはトリックでした..ありがとう.. –

0
ASCその後、タイトルをarray_multisortに渡すソート列とそのデータのリストを取得します$ mylistという

に配列のあなたの配列を割り当てます

私はこのようなものに行きます:

$new_data = array(); 

foreach($data as $v) 
{ 
    if(!isset($new_data[strtotime($v['date_test'])])) 
    { 
     $new_data[strtotime($v['date_test'])] = array(); 
    } 

    if(!isset($new_data[strtotime($v['date_test'])][$v['last_name']])) 
    { 
     $new_data[strtotime($v['date_test'])][$v['last_name']] = 0; 
    } 

    ++$new_data[strtotime($v['date_test'])][$v['last_name']]; 
} 

ksort($new_data); 

foreach($new_data as $date=>&$names) 
{ 
    ksort($names); 

    foreach($names as $name=>$amount) 
    { 
     echo str_repeat(date('Y-m-d', $date).' '.$name."\n", $amount); 
    } 
} 
0

このコードセットを試してください。あなたはそれを通過するので、ステップを理解してください。結果としてが出力されます。

<?php 
/* =========================== Dataset ============================= */ 
$data_set = array(
    '0' => array 
    (
    'date_test' => "2016-01-01", 
    'last_name' => "Smith", 
), 
    '1' => array 
    (
    'date_test' => "2015-01-01", 
    'last_name' => "Davis", 
), 
    '2' => array 
    (
    'date_test' => "2015-01-01", 
    'last_name' => "Smith", 
) 
); 

print_r($data_set); //Check the data order 
/* ========================= End Dataset =========================== */ 

//Order by alpha -------------------------------------------------------------- 
usort($data_set, function ($elem1, $elem2) { 
    return strcmp($elem1['last_name'], $elem2['last_name']); 
}); 

print_r($data_set); //Check the data set 

/* =========================== End order by alpha ============================= */ 

//Order date by ASC order ----------------------------------------------------- 
foreach ($data_set as $key => $part) { 
     $sort[$key] = strtotime($part['date_test']); 
} 
array_multisort($sort, SORT_ASC, $data_set); //SORT_ASC sets date_test to be order in the reverse direction where SORT_DESC does the complete opposite 

print_r($data_set); //Check data set once again 

/* ============================ End order by date ============================= */ 
echo "<br><br>"; 

foreach ($data_set as $var){ 
echo "\n", $var['last_name'], "\t\t", $var['date_test']; echo "<br>"; 
} 


/* ---- Final Dataset 

Array ([0] => Array ([date_test] => 2016-01-01 [last_name] => Smith) [1] => Array ([date_test] => 2015-01-01 [last_name] => Davis) [2] => Array ([date_test] => \ 
2015-01-01 [last_name] => Smith)) Array ([0] => Array ([date_test] => 2015-01-01 [last_name] => Davis) [1] => Array ([date_test] => 2015-01-01 [last_name] => Sm\ 
ith) [2] => Array ([date_test] => 2016-01-01 [last_name] => Smith)) Array ([0] => Array ([date_test] => 2015-01-01 [last_name] => Davis) [1] => Array ([date_t\ 
est] => 2015-01-01 [last_name] => Smith) [2] => Array ([date_test] => 2016-01-01 [last_name] => Smith)) 

Davis 2015-01-01 
Smith 2015-01-01 
Smith 2016-01-01 

---- Final Dataset */ 
?> 

所望の出力:

Davis 2015-01-01 
Smith 2015-01-01 
Smith 2016-01-01 

enter image description here