Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explaining nested arrays to a programmer [closed]

How have you explained nested arrays to a programmer. I'm thinking someone that has an entry level understanding of programming, but is trying to do more complicated coding.

The array with array works, but they can't quite get their mind around the idea.

Edit: example of a nested array:

array(
    'array1' => array(
        'key1' => 'val1',
        'key2' => 'val2',
    ),
    'array2' => array(
        'key1' => 'val1',
        'key2' => 'val2',
    ), 
);

Of course, they are usually more complicated than this and maybe that's the problem.

like image 402
Darryl Hein Avatar asked Nov 04 '08 01:11

Darryl Hein


2 Answers

Tell them to think of an array as a list- it helps to give them something less abstract, like a grocery list. Then, a nested array is simply a list of lists.

Maybe I have a todo list, a grocery list, and a wishlist at amazon.com . Now I have a list of all of my lists, and I can look at all of those elements in each list by stepping through them.

like image 110
Tim Howland Avatar answered Oct 12 '22 04:10

Tim Howland


A nested array is a set within a set. So, a library has a set of books, a book has a set of chapters. A chapter has a set of paragraphs, a paragraph has a set of sentences. A sentence has a set of words.

For each book in library

    For each chapter in book

        For each paragraph in chapter

etc...

like image 28
thursdaysgeek Avatar answered Oct 12 '22 05:10

thursdaysgeek