2009-08-31 4 views
-2

내가 가진이 같은 문자열 :전원을 켜고 문자열 배열 내의 하위 값을 정렬 할

이벤트 이름 | 이벤트 설명 | 이벤트 유형 | 이벤트 날짜 | 이벤트 시간 | 이벤트 정보,
이벤트 이름 | 이벤트 설명 | 이벤트 유형 | 이벤트 날짜 | 이벤트 시간 | 이벤트 세부 사항,
이벤트 이름 | 이벤트 설명 | 이벤트 유형 | 이벤트 날짜 | 이벤트 시간 | 이벤트 세부 사항,
이벤트 이름 | 이벤트 설명 | 이벤트 유형 | 이벤트 날짜 | 이벤트 시간 | 이벤트 세부 정보,

이것은 모두 하나의 문자열입니다. 그것을 배열로 바꾸고 이벤트 날짜별로 다시 정렬 한 다음 다시 문자열로 변환해야합니다.

도움을 주시면 감사하겠습니다. 정말 고마워.

답변

0
<?php 
$string = "event name|event description|event type|2009-08-01|event time|event details, 
event name|event description|event type|2009-08-02|event time|event details, 
event name|event description|event type|2009-08-01|event time|event details, 
event name|event description|event type|2009-08-03|event time|event details,"; 
    $arr = array(); 
    $strs = explode(',', $string); 
    print_r($strs); 
    foreach ($strs as $i => $str) 
    { 
     if (empty($str)) continue; 

     $expl = explode('|', $str); 
     $arr[strtotime($expl[3]) . $i] = $str; 
    } 
    ksort($arr); 
    $result = implode(",\n", $arr); 
    print_r($result); 
?> 

출력 :

event name|event description|event type|2009-08-01|event time|event details, 
event name|event description|event type|2009-08-01|event time|event details, 
event name|event description|event type|2009-08-02|event time|event details, 
event name|event description|event type|2009-08-03|event time|event details 
+0

하나 개의 루프 : – inakiabt

+0

정말 고마워요! 이것은 나를 위해 완벽하게 작동합니다. $ result = implode (", \ n", $ arr)의 끝에 쉼표를 추가해야했습니다. 이 $ result = implode (", \ n", $ arr)와 같이. ','; 그 다음엔 완벽 해. –

+0

당신이 그것을 좋아한다고 기뻐합니다 – inakiabt

1

먼저 문자열을 줄로 나누고 하위 배열로 분할하고 usort를 실행 한 다음 모두 다시 결합하고자합니다. 예를 들면 :

function lineSplit(&$item) 
{ 
    $item = explode('|', $item); 
} 

function lineSort($item1, $item2) 
{ 
    return strcmp($item1[ 3 ], $item2[ 3 ]); 
} 

function lineJoin(&$item) 
{ 
    $item = join('|', $item); 
} 

$str = '...'; 

// First split on the comma to get each line. 
$lines = explode(",\n", $str); 

// Now split each line into subarrays 
array_walk($lines, 'lineSplit'); 

// Perform the sort using a user-defined function. 
usort($lines, 'lineSort'); 

// Now join the subarrays into strings. 
array_walk($lines, 'lineJoin'); 

// And finally merge the lines again. 
$str = join(",\n", $lines); 
2
function sortStringByDate($str) 
{ 
    $arr = explode(",\n", $str); 
    foreach ($arr as $key => $val) 
    { 
     $arr[$key] = explode('|', $val); 
    } 
    $new_arr = array(); 
    foreach ($arr as $i => $vals) 
    { 
     $time = strtotime($vals[3].' '.$vals[4]); 
     $new_arr[$time] = $vals; 
    } 
    ksort($new_arr); 

    foreach ($new_arr as $key => $value) 
    { 
     $almost[] = implode('|', $value); 
    } 

    return implode(",\n", $almost); 
} 
0
$arr = explode(',', $inputString); 
$map = array() 
foreach ($arr as $line){ 
    $lineArray = explode('|', $line); 
    // convert date value to numerical representation of date+time 
    $lineArray[3] = ... 
    $map[] = $lineArray; 
} 

array_multisort($map[, ...]); 

$result = array() 
foreach($map as $lineArray){ 
    $result[] = implode('|', $lineArray); 
} 

// the resulting string: 
implode(',', $result) 
+0

필드 중 하나에 쉼표가 포함되어 있으면 조심해야합니다. –