Created
May 23, 2012 10:50
-
-
Save rafeca/2774545 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function _buildParams(array $data = array(), $preffix = '') | |
{ | |
$query = array(); | |
foreach ($data as $k=>$v){ | |
if (is_array($v)){ | |
$query[] = _buildParams($v, $preffix ? $preffix .'[' . (is_int($k) ? '' : $k) . ']' : $k); | |
} else if ($preffix) { | |
$query[] = urlencode($preffix . '['. (is_int($k) ? '' : $k) .']') . '=' . urlencode($v); | |
} else { | |
$query[] = urlencode($k) . '=' . urlencode($v); | |
} | |
} | |
return implode('&', $query); | |
} | |
class BuildParamsTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testPushAndPop() | |
{ | |
$output = _buildParams(array('param' => array(1,2,3, array(4,5)), 'lala' => '1', 'lele' => array('lolo' => '2'))); | |
$expected = str_replace('[', '%5B', str_replace(']', '%5D', 'param[]=1¶m[]=2¶m[]=3¶m[][]=4¶m[][]=5&lala=1&lele[lolo]=2')); | |
$this->assertEquals($expected, $output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment