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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:34:09+00:00 2026-05-14T18:34:09+00:00

Ok, I have an array like so, but it’s not guaranteed to be laid

  • 0

Ok, I have an array like so, but it’s not guaranteed to be laid out in this order all of the time…

$array = array(
    'sadness' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'value',
    ),
    'happiness' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'the value',
    ),
    'peace' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'the value',
    )
);

Ok, and I’d like to throw in this array right after the happiness key is defined. I can’t use the key of “peace” since it must go directly after happiness, and peace might not come after happiness as this array changes.

So here’s what I need to add after happiness…

$another_array['love'] = array(
    'info' => 'some info',
    'info2' => 'more info',
    'value' => 'the value of love'
);

So the final output after it gets inputted directly after happiness should look like this:

$array = array(
    'sadness' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'value',
    ),
    'happiness' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'the value',
    ),
    'love' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'the value of love',
    ),
    'peace' => array(
        'info' => 'some info',
        'info2' => 'more info',
        'value' => 'the value',
    )
);

Can someone please give me a hand with this. Using array_shift, array_pop, or array_merge doesn’t help me at all, since these go at the beginning and at the end of the array. I need to place it directly after a KEY position within $array.

Thanks 🙂

  • 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-14T18:34:10+00:00Added an answer on May 14, 2026 at 6:34 pm

    You are trying to have an array with two identical keys 'love'. This is not possible.

    EDIT:

    You can do:

    $new_array = array();
    foreach($array as $k => $v) {
            $new_array[$k] = $v;
            if($k == 'happiness') {
                    $new_array['love'] = $another_array['love'];
            }
    }
    

    working example

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

Sidebar

Related Questions

I'm not sure how difficult this but I have an array and would like
I have an array like this var array = [{ order: 3, sub -
So I have an array like this: foreach($obj as $element){ //do something } But
First of all, i have an output array like this (in the matrix of
This may be hard to explain, but I have array object like this: myobject
I have my array displayed like it but I cant seems to be able
So, I have a string array of some random saying and the like, but
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]

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.