drlnwji79147769 2017-06-18 17:38
浏览 24
已采纳

保持多维数组的语义?

Today's the first day I've really gotten stuck into creating anything from scratch in PHP7. My script skills are pretty rudimentary anyway, so I apologise in advance.

I've been looking at tutorial pages + searching Google/Stack and can't seem to find the answer to a few basic syntax questions about Multidimensional Arrays (MDAs).

The tutorials I've read show MDAs as follows:

MDA Tutorial Example:

http://webcheatsheet.com/php/multidimensional_arrays.php https://www.w3schools.com/php/php_arrays_multi.asp

$a = array(array(array("a", a2, a3),
                 array("b", b2, b3),
                 array("c", c2, c3) 
                 ),
           array(array("a", a2, a3),
                 array("b", b2, b3),
                 array("c", c2, c3) 
                 ),
           array(array("a", a2, a3),
                 array("b", b2, b3),
                 array("c", c2, c3) 
                 )
           );

As all the layers are simply named array and W3 only calls the parent variable with numerical values for [$row] [$col] it's unclear what solutions there are for naming and labeling elements in the array with custom handles.

Example 6 on PHP.net shows something that looks along the right lines, but it's not an exact demonstration.

Using the array shorthand which I'm more familiar with, would something like this work?

For Example:

    $a = [Custom1[Custom2[Custom3["a", a2, a3],
                         Custom3["b", b2, b3],
                         Custom3["c", c2, c3] 
                         ],
    //**does each level require the same naming as the level above
                 Custom2[Custom3["a", a2, a3],
                         Custom3["b", b2, b3],
                         Custom3["c", c2, c3] 
                         ],
    //**Or Are they independent and customised freely            
                    Fred[Wez["a", a2, a3],
                         Otto["b", b2, b3],
                         Sam["c", c2, c3] 
                         ]],
                   ];

I'm sure this is a fairly basic question, but as I said, I just couldn't seem to find an answer.

  • 写回答

1条回答 默认 最新

  • douliao5942 2017-06-18 18:07
    关注

    I think the concept you are looking for is simply "associative arrays".

    You could create something like this (trying to use your example).

    $a = [
        'Custom1' => [
            'Fred' => [
                'Wez' => ["a", a2, a3],
                'Otto' => ["b", b2, b3],
                'Sam' => ["c", c2, c3]
            ]
        ]
    ];
    

    To access to an element here you have to do it this way:

    $element = $a['Custom1']['Fred']['Otto'][2]; // Last array is numeric
    // Now $element has b3.
    

    Remember you can't use the same index more than one time into each array. So you couldn't do this:

    $a = [
        'Custom1' => [
            'Custom2' => [],
            'Custom2' => []
        ]
    ];
    

    Here you can read more about php arrays.

    Also, as said in comments, array() is just a function you use to create the array, but your are not naming it in any way. Since PHP 5.4, you can use [] notation, which I in my opinion, keeps the code cleaner.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大