Jump to content

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:

I wouldn't. It looks fine to me: even at a glance I see three distinct array keys being used at the top level, and the first and third only need another half-second to see they're using their own array keys.

Thanks requinix! I might add a space character before and after the $row variables. That should hopefully make those more distinguishable.

 

Note that I would be happy to hear any additional feedback.

Edited by cyberRobot

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.

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
    ];
}

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.

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.