Jump to content



Photo
- - - - -

Multidimensional Array


  • Please log in to reply
2 replies to this topic

#1 phpRob

phpRob

    Advanced Member

  • Members
  • PipPipPip
  • 58 posts
  • LocationCardiff, South Wales

Posted 2 November 2011 - 7:12 AM

Hi,

I'm working through Chapter 7 'Arrays'. Having read about multidimensional arays on w3schools they suggest writing them like so:

$books = array(
"PHP for the web" => array(1 => "Numbers", "Variables", "Arrays"),
"PHP and MySQL" => array(1 => "Databases", "Intro to SQL"),
"PHP Advanced" => array(1 => "Introduction to OOP", "Advanced OOP", "Frameworks")
);

Larry, do you still object to writing multidimensional arrays this way (nested)? What do you mean when you say it's too easy to make syntactical errors as the statement becomes more and more nested?

Thanks
  • 0

#2 Josee

Josee

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts

Posted 2 November 2011 - 8:10 AM

I'm not Larry, but as a fellow PHP-learner I would say that it's much easier to forget a parenthesis or a comma with nested arrays. So, even if it would seem quicker to write it all in one statement, we may just as well waste time looking for a missing comma.
  • 0

#3 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,525 posts
  • LocationState College, PA (USA)

Posted 2 November 2011 - 10:25 AM

Thanks for the question. I don't categorically advise against writing multidimensional arrays in that way, just for beginners. Once you're quite adept at PHP programming, and are unlikely to make syntactical mistakes, it may make more sense to do things this way. But definitely not when you're first starting.
  • 0