Link to home
Start Free TrialLog in
Avatar of Charles Baldo
Charles BaldoFlag for United States of America

asked on

Novice PHP Question on Objects and Arrays

I have this in PHP
       $results['News'] = [
           'Channel8' => [
               'News' => 'Walter Cronkite',
               'Weather' => 'Robert Blizzardo',
               'Sports' => 'Jane Goalie'
           ],           
           'Channel10' => [
               'News' => 'James Newsworth',
               'Weather' => 'Mary Storm',
               'Sports' => 'Joe Puck'
           ],
           'Channel13' => [
               'News' => 'Helen Worldview',
               'Weather' => 'Mary Rainer',
               'Sports' => 'Joe Gridiron'
             ]           
           ];

Open in new window

I want to echo "Mary Storm"

How do I do it and can somone give me a good PHP tutorial on Objects and Arrays in PHP
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Charles Baldo

ASKER

Extremely helpful Thank you
I'll add one more thing, since those articles were written about 9 years ago. Nowadays, a lot more platforms are on PHP 7, which is favoring object-oriented programming more and more. In PHP 7, we see a lot better / efficient internal handling of objects. They take up less memory than they used to and generally run faster than they used to.

However, objects still tend to be heavier than arrays in terms of memory. For example, if I recreated your above News structure purely in generic stdClass objects, the result would take up about 2,000 bytes of memory, while the array takes up just over 400 bytes. If I created custom classes for the structure, then the objects take up about 440 bytes, which is pretty similar to the array.

For now, as you're learning, you should use arrays when you don't know what the data might look like ahead of time (e.g. there's an unknown number of elements about to be loaded into the array, or the structure could be all over the place). You should use objects when you have a good idea of what you want the data to look like.

For example, if EVERY channel should always have a News, Sports, and Weather position (even if they are vacant), then a custom object class would be a good way to hold that data.

Meanwhile, you have channels 8, 10, and 13, but what if you add 100 different channels later on? That's something where an array would work better. So in this scenario, a mix of array and object would work well - you could have an array of channels, but each channel is an object.

At this point, the number of bytes might seem a little irrelevant or advanced, but if you're ever working with large volumes of records (let's say you're importing or exporting a million small records), it can mean the difference between PHP requiring 100 megabytes of memory or 2 gigabytes of memory (just because the effect is multiplied by the number of records).
Thank you, after playing with this for a few days I see what you are saying