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 7766713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:29:29+00:00 2026-06-01T15:29:29+00:00

i have an associative array in PHP. $myarray = array( a=>News, b=>Articles, c=>images );

  • 0

i have an associative array in PHP.

$myarray = array(
                  "a"=>"News",
                  "b"=>"Articles",
                  "c"=>"images"
                );

I want to insert some values after “a” key. so that the structure of array becomes

$myarray = array(
           "a"=>"News",
           "j"=>"Latest News",  
           "k"=>"Sports News",
           "l"=>"Entertainment",  
           "b"=>"Articles",
           "c"=>"images"
           );

How can i get this functionality.

  • 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-06-01T15:29:30+00:00Added an answer on June 1, 2026 at 3:29 pm

    The function for this is array_splice, but you are going to have to do some work manually because it does not preserve keys. Let’s make it do that:

    function search_and_insert($input, $afterKey, $newItems) {
        $keys = array_keys($input);
        $insertPosition = array_search($afterKey, $keys);
        if ($insertPosition === false) {
            return false;
        }
        ++$insertPosition;
    
        $newKeys = array_keys($newItems);
        array_splice($keys, $insertPosition, 0, $newKeys);
        array_splice($input, $insertPosition, 0, $newItems);
        return array_combine($keys, $input);
    }
    

    The idea here is that you process keys and values separately, splicing once for each array, and afterwards use array_combine to get the end result. Another good idea would be to write a reusable array_splice_assoc function using the same technique and use that instead of having one to do this specific job only.

    Usage:

    $myarray = array("a"=>"News", "b"=>"Articles", "c"=>"images");
    $newItems = array("j"=>"Latest News", "k"=>"Sports News", "l"=>"Entertainment");
    $insertAfter = "a";
    
    print_r(search_and_insert($myarray, "a", $newItems));
    

    See it in action.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an associative array that I want to display using TileList. However, it
I've got an associative array in php that I want to stick into a
In PHP, say that you have an associative array like this: $pets = array(
I have a an associative array in php that i parse to get json
Possible Duplicate: Sorting an associative array in PHP I have an array that looks
I've got an associative array in PHP and want to select a random key/value
I have an associative array in the form key => value where key is
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have a standard associative array in PHP. what's the simplest way to get
I have a PHP multidimensional associative array with the following structure: Fields: TYPE -

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.