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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:42:18+00:00 2026-05-15T14:42:18+00:00

I have an object, and I have a new value which I need to

  • 0

I have an object, and I have a new value which I need to add to this object, but not just to the end of it, that would be no problem accomplishing. What I need to do is add this value to a new node on a specific index.

Ok so here’s the object.

[object]=>{ 
  ["a"]=> "value" 
  ["b"]=> "value" 
  ["c"]=> "value"
  ["d"]=> "value"
} 

Then I have this fifth value that I now need to add to index 1, or as it’s named in this example “b”. Like this.

[object]=>{ 
  ["a"]=> "value"
  ["e"]=> "fifth value"
  ["b"]=> "value" 
  ["c"]=> "value"
  ["d"]=> "value"
} 

So the question remains, is there a smart way to do this or do I have to split them up and make them into arrays and merge them as arrays then assign them to a new object explicitly telling it to be an object?

Like this.

$object = (object)array_merge((array)$first, (array)$second);

I feel that there must be a better way to handle this, with only objects.

Thank you for your time.

  • 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-15T14:42:18+00:00Added an answer on May 15, 2026 at 2:42 pm

    Objects simply don’t have indexes, they have properties, and $obj->property is always $obj->property – no ordering, no indexes nothing like that.

    What you need here is (generally) a numerically indexed array, not an object, and not an associative array – if you insist you need an alpha sorted associative array (alpha keys rather than numerical keys) then you can use ksort to handle this particular problem.

    $array = array( 
      "a" => "value",
      "e" => "fifth value",
      "b" => "value",
      "c" => "value",
      "d" => "value",
    );
    ksort($array);
    print_r($array);
    

    note you can always turn it in to an object if for some reason you need this by doing $obj = (object)$array; after the sort

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

Sidebar

Related Questions

Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a simple jasper report that does not need a datasource so I
I have List object filled with instances of a custom struct. list.Add(new Mail(mail1, test11,
I have a new object with a collection of new objects within it on
Does a new SessionFactor y and Session object have to be created for each
Trying to do an insert, I have: jdbcTemplate.update(insert into...., new Object[]{foo.getId(), foo.getName()}) foo.getId() returns
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
I have an associative array (object) of the following construction var menu = new
I'm relatively new to the Component Object Model specification - I have a simple

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.