Make PHP multidimensional arrays saner
May 11, 2012 4:22 PM   Subscribe

Is there a PHP IDE that makes dealing with large multidimensional arrays easier? If not, does anyone have a coding style that has made these arrays easier to deal with?

I'm recently started working with CakePHP. It's been a fairly easy adjustments, but I have a hard time reading/coding larger multidimensional arrays. I tried coding the arrays as if they were if statements, but the IDE I'm using (Netbeans) doesn't autoindent parentheses the way it does brackets, and the indentation can take up crazy amounts of horizontal space.
Preferably, I'd like an IDE that simplifies this, but if someone has a coding style that works, I'd love to hear about it!
posted by yorick to Computers & Internet (7 answers total) 2 users marked this as a favorite
consider putting your multidimensional data in another format than php code, like something you export out of a spreadsheet (if its columnar data). if it's more free-form, a format like json or yaml might be easier to deal with.
posted by jimw at 4:39 PM on May 11, 2012

How much data are we talking about here? Anything more than a few hundred bytes or so should be in a database or at the very least a data file.

With that caveat, who cares how much horizontal space the data is taking up? You just have to keep it straight so you can make sure there aren't any errors. Just indent it out as necessary, and damn the pretty print.
posted by ob1quixote at 5:09 PM on May 11, 2012

This is a "feature" of CakePHP ... it likes to give you data in giant multi-dimensional arrays.

The best long-term solution is to use a PHP framework that isn't garbage (Lithium, fuel, and a host of others provide far saner data manipulation and presentation strategies).

The best short-term solution might be to use references to deeper places in the main array. Or maybe encapsulate those arrays in some sort of object.
posted by toomuchpete at 5:37 PM on May 11, 2012

I assume you mean a deeply nested hierarchical structure inside of associative arrays, rather then something like a giant N-dimensional hypercube of uniformly typed scalar values (which I have no idea why you would ever be dealing with in PHP :P)

Anyway, you should really avoid using them directly if they're getting too complicated. If you have a large, nested hierarchical structure you should be storing things in classes and objects. Also, php is usually used with databases, so it might be better to just put most of your stuff in a MySQL database and do queries to get what you need for now.

Using an associative array is really a shortcut to for making an 'object' of some type without actually sitting down and defining it's class. It takes less time to write code that way, up to a point. But if you try to rely only on nested arrays you'll end up wasting time because you'll have to keep track of the whole structure, and there's no checking to make sure you're using the right keys with the right nested level.

Also, remember you can take out peices of the array and deal with them seperately.

So rather then doing something like

$q = $bigarray[$x][$y][$bigarray[$x][$a][$b]][$z]

You can do something like

$xpart = $bigarray[$x]
$xab = $xpart[$a][$b]
$q = $xpart[$y][$xab][$z]

That should solve your problem of too much horizontal space.
posted by delmoi at 5:46 PM on May 11, 2012

Response by poster: Just to clarify, the issue with CakePHP is that the parameters for many of their built in classes/helpers are associative arrays filled with even more associative arrays. When I code from scratch, I definitely avoid that kind of bloated data structure.

I'm open to recommendations regarding other frameworks (fuel looks cool), but I'm stuck with Cake for this particular project & I'd like to make it more pleasant.
posted by yorick at 6:35 PM on May 11, 2012

I tend to break lines after the opening array(, indent one level (I like two spaces for PHP) after that, and align things in columns on the =>. For nested arrays, I add a line of whitespace around sub-arrays. A concrete example:
    public $defaultRoutes = array(

      'GET' => array(
        'id/action'   => null,
        'bson/action' => null, 
        'action'      => null, 
        'id'          => 'view',        
        'default'     => 'index',     
      'POST' => array(
        'id/action'   => null,
        'bson/action' => null, 
        'action'      => null, 
        'id'          => 'update',      
        'default'     => 'create',    
      'DELETE' => array(
        'id'          => 'delete',      
        'bson'        => 'delete',    
If it gets much gnarlier than that, it can help to compose them in chunks rather than declaring all at once.

I don't really do IDEs, but Vim plus a couple of plugins (Align, in particular) make it not-too-gnarly to keep things tidy.
posted by brennen at 7:00 PM on May 11, 2012 [1 favorite]

Just to clarify, the issue with CakePHP is that the parameters for many of their built in classes/helpers are associative arrays filled with even more associative arrays. When I code from scratch, I definitely avoid that kind of bloated data structure.
Yeah, I'd say just just break up your arrays into smaller chunks, and references those chunks as variables when you're building it. That will keep things to a minimum number of nesting levels, and should hopefully keep things easier to read.

You might want to wrap the huge structures in wrapper classes, so you call a function on your wrapper class, and that function modifies the underlying array. Or another strategy would be to create a "builder" class, that allows you to modify data using named functions, then you'd call a "tooArray()" function at the end to build the array.
posted by delmoi at 4:28 AM on May 14, 2012

« Older Renters' Rights After Burst Water Heater in Apt...   |   Old-timer web designer looking to get back up to... Newer »
This thread is closed to new comments.