Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6661245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:15:20+00:00 2026-05-26T02:15:20+00:00

I have a string like this, sidePanel[]=1&sidePanel[]=2&sidePanel[]=4 And if I need to add another

  • 0

I have a string like this,

sidePanel[]=1&sidePanel[]=2&sidePanel[]=4

And if I need to add another value to the string I do this:

$sidePanel = explode('&', $_SESSION['sidePanel']);
array_push($sidePanel, 'sidePanel[]=3');
$sidePanel = implode('&', $sidePanel);

Which returns this:

sidePanel[]=1&sidePanel[]=2&sidePanel[]=4&sidePanel[]3

Now how can I make it so that it will always insert after the array ‘sidePanel[]=2’ when I explode it at &?

I do not want it in numerical order although this example will return it in numerical order, as the string can change to be in any order.

I cannot use array_splice as I understand this uses the key of the array, and as the position of sidePanel[]=2 can change it will not always be the same.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:15:21+00:00Added an answer on May 26, 2026 at 2:15 am

    You can indeed use array_splice, but you have to find the position of your insertion point first:

    $sidePanelArr = explode( '&', $_SESSION['sidePanel'] );
    // find the position of 'sidePanel[]=2' in array
    $p = array_search( 'sidePanel[]=2', $sidePanelArr );
    // insert after it
    array_splice( $sidePanelArr, p+1, 0, 'sidePanel[]=3' );
    $sidePanelSt = implode( '&', $sidePanelArr );
    

    You could also splice the string right into your original string without exploding and re-imploding.
    The function substr_replace() is your friend:

    $sidePanelSt = $_SESSION['sidePanel'];
    // find the position of 'sidePanel[]=2' in string
    // (adding '&' to the search string makes sure that 'sidePanel[]=2' does not match 'sidePanel[]=22')
    $p = strpos( $sidePanelSt.'&', 'sidePanel[]=2&') + strlen('sidePanel[]=2' );
    // insert after it (incl. leading '&')
    $sidePanelSt = substr_replace( $sidePanelSt , '&sidePanel[]=3' , $p, 0 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a String Like This: Dark Bronze - add $120.00 I need to
I have a string like this that I need to parse into a 2D
I have a string like this: SELECT NOW(),DATE_ADD(NOW(), INTERVAL -11 day) and I need
I have a string like this ch:keyword ch:test ch:some_text I need a regular expression
I have string like this: http://someurl.com?test&lettersg and I would like to mach part from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a string like this: $string = 1,4|2,64|3,0|4,18|; Which is the easiest way
I have a string like this: http://www.downlinegoldmine.com/viralmarketing I need to remove http://www. from the
i have a string like this : string_data = <option value='1'>ahmad</option><option value='2'>mohammad</option><option value='3'>ali</option> now
Here I have string like this 1322485986.672901000 , data type is string which represents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.