cyberRobot Posted November 23, 2016 Share Posted November 23, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/ Share on other sites More sharing options...
requinix Posted November 23, 2016 Share Posted November 23, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/#findComment-1539619 Share on other sites More sharing options...
cyberRobot Posted November 23, 2016 Author Share Posted November 23, 2016 (edited) 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 November 23, 2016 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/#findComment-1539620 Share on other sites More sharing options...
maxxd Posted November 23, 2016 Share Posted November 23, 2016 Personally, I don't find it confusing at all - I'll often write statements in the same manner. Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/#findComment-1539635 Share on other sites More sharing options...
requinix Posted November 23, 2016 Share Posted November 23, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/#findComment-1539637 Share on other sites More sharing options...
kicken Posted November 24, 2016 Share Posted November 24, 2016 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 ]; } Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/#findComment-1539643 Share on other sites More sharing options...
cyberRobot Posted November 28, 2016 Author Share Posted November 28, 2016 Thanks for all the feedback! 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. Quote Link to comment https://forums.phpfreaks.com/topic/302601-programming-style-for-multidimensional-associative-arrays/#findComment-1539772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.