PhpSnip.com

User Stats

URL string from array of variables

A simple function that takes an array such as HTTP_GET_VARS and creates an url variable string from the elements. This string can then be used in a link to another page. Especially useful for Last Page / Next Page / Modify Search buttons on search pages.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 490

Source Code ( 32 lines )

function array_to_url( $values ){

	if( !is_array( $values ) ) return false;
	
	$url = '';
	
	reset( $values );

	while( list( $k, $v ) = each( $values ) ){
	
		if( !is_object( $v ) ){
		
			if( is_array( $v ) ){
			
				reset( $v );
				
				while( list( $k2,$v2 ) = each( $v ) ){
					$url .= $url == '' ? $k.'['.$k2.']='.urlencode( $v2 ) : '&'.$k.'['.$k2.']='.urlencode( $v2 );
				}
				
			}
			else{
				$url .= $url == '' ? "$k=" . urlencode( $v ) : "&$k=" . urlencode( $v );
			}

		}

	}
	
	return $url;

}

Search

Subscribe

  Rss Feeds

Sponsors

Advertise