What is an array property?

What is an array property?

If I were to explain what I know thus far to someone the outside looking in, I would tell them a "key" is like a column name and the "value" is the value associated with that column.

I've been working my way through an array that's loaded with all kinds of bells and whistles and I've got this configuration:

$result = array('stuff'=>array())

I've heard "stuff" referred to as a "key," I've also heard it referred to as a "property." Which is it and, while I've googled the snot out of "array property," I've yet to understand what it is.

What is an array property and could you provide a link to a resource that defines it?

Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Bruce,

Terminology can be a pain at times, so here goes my explanation.

An array is a collection of things, and in order to identify those things in the collection, you need an identifier. In an array, it's called a key or an index (depending on who you talk too). I generally refer to it as a key if it's named, or an index if it's not. For example:

$person =array(
    'firstname' => 'Chris',
    'lastname' => 'Stanyon',

The keys are firstname and lastname, so you can access the values by key - echo $person['lastname']

You can also define an array like so:

$days = array('Sunday', 'Monday', 'Tuesday');

Here, I would refer to the identifier as an index. You haven't explicitly set it, but it's there. Array indexes start at zero (plenty of online memes about that one!!)

echo $days[2]; // Access the value at index 2 (Tuesday)

Be careful with accessing indexes bceause if you sort your array, you could potentially get unexpected results!

Now the values of entries in arrays can be pretty much anything, so your example:

$result = array('stuff'=>array())

You have a key of stuff in the $result array. The value of stuff is ... an array:

echo $result['stuff'][0] -- get's the value of the key 'stuff' and then get's the first element of that array by an index!

Making sense so far ?

Now when it comes to properties - they're generally not related to arrays (although some people may use the term). Generally, when we talk about properties, we're talking about Object Oriented Programming (OOP). OOP revolves around classes and objects. An object has properties, so when I think of an array property, I'm think of a property on an object that is an array

class MyObject
    // define array properties
    public $days = array('Monday', 'Tuesday', 'Wednesday')
    public $user = array('firstname' => 'Chris', 'lastname' => 'Stanyon');

Open in new window

So MyObject has 2 array properties, and they can be used like so:

$obj = new MyObject();
echo $obj->days[2]; // Tuesday
echo $obj->user['lastname']; // Stanyon

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
Chris, this makes sense.

I'm still weeding through that mess you helped me out with weeks ago. I'm determined to "master" this stuff, but it gets tricky when different people use different terms to describe the same thing.

I'm going to explain this back to you and you give me a thumbs up if I'm nailing it.

First off, the term "properties" is better suited for those elements within a class. I understand what you're saying, as far as arrays being a property within a class and I can see why they would be referred to as such.


When you're talking about the elements within an array itself, specifically in the context of the example I cited ($result = array('stuff'=>array())), in that instance, you're probably being a little more clear by referring to "stuff" as a "key" as opposed to a "property."

Bruce GustPHP DeveloperAuthor Commented:
Let me know if what I explained back to you is accurate.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Chris StanyonWebDevCommented:
You got it Bruce. Bang on!

The 'stuff' in your array is a key, and if $result was part of a class, that would be a property - "I need the 'stuff' key from the 'result' property"

I know it can be tricky when people use different terms (often incorrectly).

In development, think about properties as being related to OOP, so they belong in classes. FYI - Functions within classes are referred to as Methods! It helps distinguish OOP specific code.
Bruce GustPHP DeveloperAuthor Commented:
I did find this, but I think at this point, you're getting into some weeds that can make this far more difficult than it needs to be...


Bottom line: Key-> Value BOOM!

You start talking about "properties" and at that point, you're either discussing elements within a method / class or you're getting into some details that aren't usually engaged in the context of regular programming.

Chris StanyonWebDevCommented:
Yeah - all (most?) programming languages have arrays. They're a fundamental part of a language and you'll find yourself using them A LOT!

In very simply terms an array contains a collection of key -> value pairs (that value can be any valid datatype, including arrays if needed!). Generally, this is called an associative array. If you don't have the keys, as in the $days example above, then it's called an indexed array .... or just an array.

As an example, imagine querying your DB for all Customers. Often what is returned is an [indexed] array of associative arrays! The [indexed] array is the list of customers. Each customer is an associative array, containing key -> value pairs. The key is usually the DB Column Name and the value is the data from the DB. Make sense ?

Properties are Class level - not Method level -  although you can access the Class properties from within a Class method!
Fred MarshallPrincipalCommented:
Sometimes I just like to read these threads without participating.  Is there a language or set of languages that are the context of all these posts? That context seems missing.
Chris StanyonWebDevCommented:
Hey Fred. It's posted in the PHP zone ;)
Fred MarshallPrincipalCommented:
Ah!  Thanks Chris.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.