-1

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.

10
  • Do you mean to remove a value, or add a value, then reformat the string like above? (Ex: Remove the 6 entry to read navMenu[]=1&navMenu[]=2&navMenu[]=3&navMenu[]=4&navMenu[]=5?) Commented Oct 11, 2011 at 4:19
  • Are you sure you need to do that? Can't you just get the navMenu variable from $_GET['navMenu']? Commented Oct 11, 2011 at 4:20
  • please revise grammar and punctuation. Commented Oct 11, 2011 at 4:20
  • @Ariel yes, this value gets saved into the database to save the users' layout, if one of the values is empty then it shouldn't be there but as it is in the DOM then when I serialize the form it still shows but as an empty navMenu[] Commented Oct 11, 2011 at 4:24
  • 1
    Topically related: Replace a whole integer match in a delimited string and a maximum of one of its adjacent commas Commented Aug 13, 2024 at 7:19

2 Answers 2

1
$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.

Sign up to request clarification or add additional context in comments.

Comments

-1

A side effect of parsing the querystring, filtering it, then re-encoding it is that the implied indexes (empty square braces) will be replaced with explicit indexes. Without calling array_unique() on the filtered temporary array, there will be a potential gap in the in keys - this may or may not be problematic. Also, the square braces themselves will be URL encoded (which makes the string harder for humans to read -- if it matters). https://3v4l.org/eWM9O

Alternatively, you can use a regular expression to manipulate the predictable and repetitive string without parsing it. The pattern uses a conditional expression to correctly match the leading or trailing & (but never both) and it uses a word boundary to ensure that only the whole integer is matched when filtering.

Code: (Demo with a few examples)

echo preg_replace('#(&)?navMenu\[]=' . preg_quote($find) . '\b(?(1)|&?)#', '', $str);

Largely similar content: Replace a whole integer match in a delimited string and a maximum of one of its adjacent commas

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.