2013-10-04 6 views
0

큰 배열을 작성한 다음 정렬을 수행 한 다음 정렬 된 배열의 첫 번째 n 요소를 가져와야합니다. 이처럼 GET 배열의 출력에조건을 가진 출력 배열을 만들 수 있습니까

foreach ($array_RESPONSEdata as $key => $row) { 
$new_created_time[$key] = $row['DATE_PIC']; 
$new_thumbnail[$key] = $row['LINK_PIC']; 
$new_tags_name [$key] = $row['TAG_PIC']; 
} 

:

배열을 생성

Array (
[0] => Array ([DATE_PIC] => 1376566005 [LINK_PIC] => http://distilleryimage3.s3.amazonaws.com/90ebfcc2059d11e381c522000a9e035f_5.jpg [TAG_PIC] => test) 
[1] => Array ([DATE_PIC] => 1376222415 [LINK_PIC] => http://distilleryimage9.s3.amazonaws.com/957a78a4027d11e3a72522000a1fb586_5.jpg [TAG_PIC] => test) 
[2] => Array ([DATE_PIC] => 1374685904 [LINK_PIC] => http://distilleryimage2.s3.amazonaws.com/1dbe356ef48411e2931722000a1fc67c_5.jpg [TAG_PIC] => test) 
[3] => Array ([DATE_PIC] => 1373909177 [LINK_PIC] => http://distilleryimage0.s3.amazonaws.com/0fd9b22adce711e2a7ab22000a1f97eb_5.jpg [TAG_PIC] => test) 
[4] => Array ([DATE_PIC] => 1372089573 [LINK_PIC] => http://distilleryimage0.s3.amazonaws.com/0fd9b22adce711e2a7ab22000a1f97eb_5.jpg [TAG_PIC] => test) 
[5] => Array ([DATE_PIC] => 1371468982 [LINK_PIC] => http://distilleryimage0.s3.amazonaws.com/0fd9b22adce711e2a7ab22000a1f97eb_5.jpg [TAG_PIC] => test) 
) 

다음, 키 DATE_PIC 정렬 배열 :

array_multisort($new_created_time, SORT_DESC, $array_RESPONSEdata); 

이 받기를 배열을 역순으로 배열.


질문 : 정렬 후 어떻게 처음 세 줄의 출력?

이 응용 프로그램에 맞는 경우가 실제로 시작하는 배열을 분리 할 필요가 없습니다, 이것은 일부 메모리를 절약 할 수
+0

'정렬 후 어떻게 처음 세 줄을 가정 할 수 있습니까? '- 당신이 명확히 할 수 있습니까? –

+0

'array_slice ($ array_RESPONSE 데이터, 0, 3)'? – kojiro

+0

@kojiro Oooo cool! array_slice에 대해 몰랐습니다 :) 대단히 감사합니다. 우리는 대응해야했다. –

답변

0

: 물론

<?php 

    // Will sort using 'DATE_PIC' as index. 
    array_multisort($array_RESPONSEdata, SORT_ASC); 

    // We'll define a buffer, so PHP doesn't need to echo every time (list-style). 
    $buffer = array(); 

    /** 
    * You mentioned 'get the first n elements', to specify how many 
    * rows you want to get, you can specify the max count in a for loop 
    * array_multisort reassigns numeric index keys in order after sorting. 
    * 
    * e.g. If we want 5 records to count: 
    * $max = 5; 
    * 
    * If you want all that is returned you can do this: 
    * $max = count($array_RESPONSEdata); 
    */ 
    $max = 5; 

    for($n = 0; $n < $max; $n++) 
    { 
     $buffer[] = '<ul>'; 
     $buffer[] = '<li>' . $array_RESPONSEdata[$n]['TAG_PIC'] . '</li>'; 
     $buffer[] = '<li>' . $array_RESPONSEdata[$n]['DATE_PIC'] . '</li>'; 
     $buffer[] = '<li><img src="' . $array_RESPONSEdata[$n]['LINK_PIC'] . '" alt="' . $array_RESPONSEdata[$n]['TAG_PIC'] . '"/>'; 
     $buffer[] = '</ul>'; 
    } 

    // And now the output magic (Not really): 
    echo implode("\r\n", $buffer); 
?> 

, 구조 당신의 HTML 필요에 따라, 즉에만이었다 예. 그건 그렇고, 참조 용으로 나는 소스를 다른 줄에 인쇄하기 때문에 내 버퍼를 함께 연결하기 위해 "\r\n"을 사용하여 html/css 조정을 위해 소스에서 무슨 일이 벌어지고 있는지 쉽게 볼 수 있습니다.

+0

GGGGGreat!))) 고맙습니다! 흠뻑! –

+0

전혀 문제가되지 않았으므로 찾고 있던 것이었기 때문에 기쁩니다. – Jason

관련 문제