Variable naming for multidimensional arrays (PHP)

It’s hard for me to find a PHP multidimensional array naming system, so looking at the variable name will give you a hint about the structure of a multidimensional array.

A fictional example:

$task = array('who'=>'John', 'what'=>'wash the dishes', 'priority'=>10);
$calendar[$year][$month][$day][$task_id] = $task;

In this case, I called it a “calendar”, because the whole structure has a simple meaning in general, but in most cases there is no such direct connection with any word or real concept. In addition, I used the date details here (year, month, day) for an example, but usually my keys are integer identifiers of database records.

Therefore, I would like the naming system to describe this relationship:

"year X month X day -> list of tasks"

generally:

"key1 x key2 x key3 x ... x key n -> items"

A possible naming convention using the word on and underlines as separators between keys:

$tasks_by_year_month_day = array(...); // items_by_key1_key2_key3

, , :

$tasks_by_month_day = $tasks_by_year_month_day['2010'];

$november2010Tasks_by_day = $tasks_by_year_month_day['2010']['nov'];

?

.

+3
2

, , - . , , .

, . , . :

function getTasksByDate($year, $month, $day) {
    $tasks = getTasks();
    return $tasks[$year][$month][$day];
}

, $tasks $tasks_by_year_month_day? , . , .

, . , , , - , ADT. .

, . , . , .

+1

, , / . $lut_a_to_b ( b a). , .

0

Source: https://habr.com/ru/post/1773875/


All Articles