Jump to content

Programming style for multidimensional associative arrays


cyberRobot

Recommended Posts

Does anyone have suggestions for formatting the following line:

$projects[$row['phaseNum']]['tasks'][$row['taskNum']] = 'task information';

The code works fine, but it's cumbersome. How would you write the line to improve readability?

 

Note that I don't necessarily need to use arrays. I'm open to other ideas.  :happy-04:

Link to comment
Share on other sites

I took a quick look through some a personal project to see if I could find any examples that match the sort of thing you're dealing with. Didn't quite find what I was hoping for, but here's some lines I found:

return call_user_func(self::$instance->data["crypt"]->data[$decryptor], substr($value, $decryptorlen + 2));

info("kernel.cache", ["Cache provider '%s' already registered as %s", $provider, get_class(self::$providers[$provider]["object"])]);

self::$routers[$name]["object"] = instance(self::$routers[$name]["class"], [$name, self::$routers[$name]["config"]]);
It also helps to use an editor with syntax highlighting - makes it easy (easier) to spot arrays when the "array" or []s are colored differently than variables and strings.
Link to comment
Share on other sites

I think what you have is fine. Depending on the length of the keys or whether I re-use them sometimes I will store them in separate variables, eg:

$phase = $row['phaseNum'];
$task = $row['taskNum'];

$projects[$phase]['tasks'][$task] = 'task information';

If the array starts getting deep I will alias via references. For example:

 

$stuId = $row['studentId'];
$sprId = $row['programEnrollmentId'];
if (!isset($MasterList[$stuId][$sprId])) continue;

$ses = &$MasterList[$stuId][$sprId]['sessionList'][$row['sessionName']];
if ($ses){
    $ses['enrollments'] += $row['numEnrollments'];
    $ses['isStartSession'] = max($row['isStartSession'], $ses['isStartSession']);
} else {
    $ses = [
       //blah
    ];
}
Link to comment
Share on other sites

Thanks for all the feedback!  :happy-04:

 

 

It also helps to use an editor with syntax highlighting - makes it easy (easier) to spot arrays when the "array" or []s are colored differently than variables and strings.

 

Yep, that would be useful. I'll look into switching...at some point...

 

 

If the array starts getting deep I will alias via references.

 

Thanks kicken! I figured something like this would be possible. I've passed variables by reference to functions before. I didn't think to use the technique elsewhere.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.