Last active
August 23, 2016 09:49
-
-
Save pensiero/9ef93667eaad0482b5a187d865339c84 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 elaborateParams($params, $notAllowedKey) | |
{ | |
// scorre i parametri dei filtri | |
$paramsString = ""; | |
$paramsStringWithEverything = ""; | |
$i = 0; | |
foreach ($params as $key => $param) { | |
if ($i++ == 0) { | |
$startString = ''; | |
} | |
else { | |
$startString = '&'; | |
} | |
// completa la stringa, la chiave è il nome del parametro, param è il valore | |
$paramsStringWithEverything .= $startString . $key . "=" . $param; | |
// non include un determinato parametro | |
if ($key != $notAllowedKey) { | |
$paramsString .= $startString . $key . "=" . $params; | |
} | |
// modifica l'array per poi iniettarlo nella view tramite il model | |
$params[$key . "Requested"] = $param; | |
unset($params[$key]); | |
} | |
echo "Query params: " . $paramsStringWithEverything . "\n"; | |
echo "Query params (only fruits): " . $paramsString . "\n"; | |
echo "Requested?: " . (isset($params['appleRequested']) ? 'yes' : 'no') . "\n"; | |
echo "Count: " . count($params['appleRequested']) . "\n"; | |
} | |
$params = [ | |
'apple' => 'red', | |
'ananas' => 'yellow', | |
'kiwi' => 'brown', | |
'unicorn' => 'pink', | |
]; | |
elaborateParams($params, 'unicorn'); | |
/* | |
Expeceted result: | |
Query params: apple=red&ananas=yellow&kiwi=brown&unicorn=pink | |
Query params (only fruits): apple=red&ananas=yellow&kiwi=brown | |
Requested?: yes | |
Count: 4 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment