1

In a PHP course, an example of an Associative Array is:

$myAssocArray = array('year' => 2012,
                    'colour' => 'blue',
                    'doors' => 5);

Is that a good example of an Associative Array? Why use an Array for this car, and why not use an Object? I thought arrays weren't used to list an object's properties, but rather for collections of similar values.... such as:

$numberStudents = array('firstgrade' => 78,
                    'secondgrade' => 84,
                    'thirdgrade' => 76);

Thanks!

5
  • Aren't those both arrays? Here's a discussion already on this topic, stackoverflow.com/questions/2056931/…. Commented Mar 11, 2015 at 2:43
  • 1
    I think it all depends on what you're trying to achieve. If your program is about manipulating cars, then I could see using objects regarding cars, but if your program is about something else where a car accounts for only a small part of the program then an associative array is better. Can you elaborate more on the application? What do you want to achieve in the end with the use of the object/array? Commented Mar 11, 2015 at 2:44
  • @chris85 yes the code is arrays but from what I gather, the first array is about a car and he is asking if its better to use that to describe a car or to write a whole object about one. Commented Mar 11, 2015 at 2:46
  • Thanks Mike. Here's an example, let's say I need to store my database credentials for a web app: $host = 'example.org'; $username = 'myuser'; $password = 'mypass'; There's only one set of credentials since there's only a single database. Is it better to use an Associative Array or an Object for that? Thanks! Commented Mar 11, 2015 at 2:49
  • Yup, you are correct @Mike. My mistake, read to quickly. Commented Mar 11, 2015 at 2:49

1 Answer 1

1

When you're creating your own objects/arrays to store data I believe it's a matter of preference.

My only preference is not to nest arrays within objects and vice-versa as my recent experience with an API slowed down to a snail's pace when trying to check if I need to call an index or a property traversing down 10 levels...

If you're talking about objects that contain classes/are closely tied to the database or using frameworks which provide you those objects, usually you have many helper methods inherited which allow setting/getting properties, accessing formatting functions, export functions, and many other 'goodies' that their authors forced down your throat.

In those situations obviously the objects serve a specific purpose.

My preference is to work with non-associative arrays and avoid all foreach loops as best as I can.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.