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 list of objects which I want to turn into a set.
I have a set of objects that I want to conform to an interface,
I want to annotate a list of objects (which each have a set of
I have something like this in my code: worker.setObject(queue.poll()); I want a queue that
I have a set of objects I'd like to do some operations on, in
I have an odd problem with Django. I have a set of objects that
I have several objects set up in Core Data, one of which is Deck
I have set up a Core Data model where I have two objects, say
I have a class that serializes a set of objects (using XML serialization) that
I have an API that filters a set of objects based on the ID

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.