Beginner of PHP.

$product_components = [
    'product1' =>[
        'part1' =>2,
        'part2' =>1,
    'product2' =>[
        'part3' =>3,
        'part4' =>2,
    'product1' =>[
        'part2' =>1,
        'part4' =>1,

For example,
The product product1 is
Made with 2 part 1 and 1 part 2 ...

An array like


Foreach will be used twice for this array.

foreach ($product_components as $product_id =>$product_component) {
    foreach ($product_component as $part_id =>$part_count) {

When you read this, is there anything you feel uncomfortable with each variable name in foreach?

Personally, in the second foreach, from $product_component
I think it's okay if part_count of a completely different word comes out.

By the way, first
It was $product_parts_counts
It is impossible to write all the information in 3D and 4D, so
I put it together in one word, components.

If i use foreach, isn't it possible to disassemble the word component and get part_count?

Is it wrong from the first name?

  • Answer # 1

    Personally, foreach is a few lines at most, so if you understand the meaning, you won't feel strange. In any case, if the associative array is made triple or quadruple by design, I think it is inevitable that words that are irrelevant in the outermost and innermost areas will appear.

    Rather than avoiding this by multiplexing associative arrays, I think it is better to think about creating a structure that classifies the product so that the user is not aware of the internal creation of data. The

  • Answer # 2

    Several weeks, months later, when I saw myself when I almost forgot
    Or I think it would be good to know when someone who doesn't know the situation at all saw it.

    There is also the idea that comments in code are "write to yourself in 3 days".

    For me personally, I don't care unless it is too far away or the magic number is heavy.
    Rather, I'm more concerned about what the logic was written for.