开发者

PHP string replace?

开发者 https://www.devze.com 2023-04-12 07:20 出处:网络
How can I take a string like: navMenu[]=1&navMenu[]=6&navMenu[]=2&navMenu[]=3&navMenu[]=4&navMenu[]=5

How can I take a string like:

navMenu[]=1&navMenu[]=6&navMenu[]=2&navMenu[]=3&navMenu[]=4&navMenu[]=5

And in php make it so that I can remove a certain value for the navMenu[], but it would still stay in the same order but with the value removed. Or add a value as well.

I have been tampering with exploding it at the & sign开发者_如何学运维 but am not sure how I can add or remove a value, and making sure the & sign is not at the start or end of the string.


$str = 'navMenu[]=1&navMenu[]=6&navMenu[]=2&navMenu[]=3&navMenu[]=4&navMenu[]=5';
parse_str($str, $values);
$values['navMenu'] = array_diff($values['navMenu'], array('3'));
echo http_build_query($values);

If you're getting this from the request, you don't even need parse_str, you can just get the already parsed string from $_GET or $_POST, remove the value, then use http_build_query to reassemble it into a query string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号