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.

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

报告相同问题?

悬赏问题

  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款