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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:05:52+00:00 2026-05-19T12:05:52+00:00

I have a set of objects I want to put into an array and

  • 0

I have a set of objects I want to put into an array and I want to distinguish them with keys.

The initial code I wrote was:

array_push($array[$key], new myObj(param1, param2, etc));

When I run it I get the warning:

PHP Warning:  array_push() expects parameter 1 to be array, null given in file.php on line 56

I added a var_dump() to see what was actually happening and it was filling the array with each element ‘$key => null’ as suggested by the error.

If I remove the [$key] from the line then it fills the array with instances of myObj as expected so I know that the constructor is functioning correctly and not really returning ‘null’.

  • 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-19T12:05:53+00:00Added an answer on May 19, 2026 at 12:05 pm

    Keys in associative arrays need to be unique, so if you want to keep the notion of a key/value pair where you can access things directly by the key, don’t use array_push, simply set the key (this is fine to do in PHP):

    $array[$key] = new myObj(param1, param2, etc)
    

    On the other hand, if you want to have a list of key/value pairs, like a stack, and you’re adding just one key/value pair at a time, don’t use array_push, simply add to the array using PHP’s shorthand syntax (as the manual says, this is faster as it saves you a function call):

    $stack[] = array($key => new myObj(param1, param2, etc));
    

    One more possibility, if you have a bunch of key/value pairs, and you want to add all of them to some stack like structure (but not be able to index directly by some key), then you should use array_push:

    array_push($stack,
        array($key1 => new myObj(param1, param2, etc)),
        array($key2 => new myObj(param3, param4, etc))
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of objects in a Vector from which I'd like to
I have a set of objects which I iterate through, however I may decide
I have an object that is mapped to have a set of objects, very
I have a set of tree objects with a depth somewhere in the 20s.
As described in another question , I have a set of Model objects and
I have a asp:menu object which I set up to use a SiteMapDataSource but
I have a custom security principal object which I set in the global.asax for
I have been getting an error in VB .Net object reference not set to
I'm sure we all have received the wonderfully vague Object reference not set to
We have set up a system where notifications get sent to a user with

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.