What is an array property?

Bruce Gust
Bruce Gust used Ask the Experts™
on
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?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

Bruce GustPHP Developer

Author

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.

But...

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."

Correct?
Bruce GustPHP Developer

Author

Commented:
Let me know if what I explained back to you is accurate.
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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 Developer

Author

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...

https://www.w3schools.com/jsref/jsref_obj_array.asp

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.

Yeah...?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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!
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.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hey Fred. It's posted in the PHP zone ;)
Ah!  Thanks Chris.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial