Multi-dimensional Arrays

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:08
    LEANNA PELHAM: OK.
  • 0:09
    So we have associated arrays and indexed arrays.
  • 0:12
    And, really, they're the exact same thing.
  • 0:14
    Both contain items, and each item has
  • 0:16
    unique key we can use to access it.
  • 0:19
    We choose that key for items in associative array
  • 0:22
    and we let PHP choose the keys for us in an indexed array.
  • 0:26
    And because PHP isn't very creative, it just
  • 0:29
    chooses a number that gets higher each time we add something.
  • 0:33
    But regardless of who makes the choice, every key in an array
Loading ...

Multi-dimensional Arrays

Loading...

Every key in an array is either a string or a whole number, which we programmers and mathematicians call an integer. And that's the end of the story: array keys are only ever strings or integers in all of PHP. But each value in an array can be any type of PHP value. So far we know three data types in PHP: a string, a number and an array. This means that we can have multi-dimensional arrays: an array with another one inside of it. Multi-dimensional arrays are actually pretty common and easy to use. In this lesson we're going to expand our existing array by putting the pet details inside the array. Once we have the new data in the array we'll look at how we access this nested information, and how we can count what we have in there.

Downloads: 
Log in or sign up to download companion files.
Additional resources: