본문 바로가기

php 배열 뒤집기

뭔가를 전형적으로 뒤집을 때야.


$tmp = array();

for( i = 0; length; i++ ) {

  $aaa[a] = dataarray[i][a];

  $aaa[b] = dataarray[i][b];


$tmp[] = $aaa;

}


디비 쿼리를 조회를 직접 할 수 있는 경우야 order by desc를 줘서 하면 된다.


api를 써서 오래된 글이 제일 위로 최신글이 제일 아래로 왔을 때 게시판에는 반대가 되어야 하고 마지막글은 최신글이여야 한다.


그럴 때 날라온 배열을 뒤집어서 저장시킨다.


제공하는 리버스 함수로.


array_reverse($dataarray) ;


foreach( array_reverse( $dataarray) as $item ) {

//op

 echo $item->a;

}


배열 리버스는 새 배열을 던져준다. 원본은 건드리지 않는다.



while( $aaa = array_pop( $bbb) ) {

  echo $aaa;

}


$aaa = array("a","b","c");

$idx = count( $aaa ) -1;


while( $idx > -1 ) {

echo $aaa[i];

$idx--;

}


보통은

for( $i; count( $aaa ) -1; $i >=0; $i-- ) {

echo $aaa[$i];

}