V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
banxi1988
V2EX  ›  PHP

下面两种代码写法,你喜欢哪一种?为什么?

  •  
  •   banxi1988 · 2016-01-26 22:55:26 +08:00 · 4115 次点击
    这是一个创建于 3018 天前的主题,其中的信息可能已经有所发展或是发生改变。

    第一段:

    $cate = array();
            $cate[0]['id']=1;$cate[0]['name']='种类 1';
            $cate[1]['id']=2;$cate[1]['name']='种类 2';
            $cate[2]['id']=3;$cate[2]['name']='种类 3';
            $cate[3]['id']=4;$cate[3]['name']='种类 4';
            $cate[4]['id']=5;$cate[4]['name']='种类 5';
    

    第二段:

    $cate_list = ['种类 1','种类 2',' 种类 3','种类 4','种类 5'];
    $cate = [];
    foreach($cate_list as $index => $value){
        $cate[] = ['id'=>$index + 1,'name'=>$value];
    }
    
    ``
    
    25 条回复    2016-01-31 18:53:24 +08:00
    cxbig
        1
    cxbig  
       2016-01-26 23:01:27 +08:00
    除非是初始化数据库代码,不然哪种都看着不舒服。
    xifangczy
        2
    xifangczy  
       2016-01-26 23:03:54 +08:00   ❤️ 1
    $cate = array
    (
    array('id'=>1,'name'=>'种类 1'),
    array('id'=>2,'name'=>'种类 2')
    );
    ayouwei
        3
    ayouwei  
       2016-01-26 23:04:48 +08:00
    2. 对种类增加,删除,插入,调序没 1 那么疼。
    yadam
        4
    yadam  
       2016-01-26 23:15:21 +08:00   ❤️ 1
    $cate_list = ['种类 1','种类 2',' 种类 3','种类 4','种类 5'];
    $cate = array_combine(range(1, count($cate_list)), $cate_list);
    pynix
        5
    pynix  
       2016-01-26 23:27:52 +08:00
    相比之下 2 更优雅。。。
    msg7086
        6
    msg7086  
       2016-01-26 23:59:06 +08:00
    如果 cate 非空的话,我会考虑用 array-merge 。否则直接构建数组立即量即可。
    zfj1441
        7
    zfj1441  
       2016-01-27 00:39:18 +08:00 via Android
    个人觉得第一种,代码工整直观。
    gdtv
        8
    gdtv  
       2016-01-27 00:46:33 +08:00
    如果有规律并且数量比较大的,就第 2 种,否则就第 1 种
    jsjscool
        9
    jsjscool  
       2016-01-27 01:14:37 +08:00
    array_push(); 这么好的函数你不用
    Asimov
        10
    Asimov  
       2016-01-27 06:57:47 +08:00 via Android
    我喜欢第一种,一眼望去就知道是干嘛的。
    aivier
        11
    aivier  
       2016-01-27 08:46:26 +08:00
    除非客户捉弄我,不然就写第二种
    banxi1988
        12
    banxi1988  
    OP
       2016-01-27 09:11:50 +08:00
    @jsjscool `$cate[] = XX;` 这种写法一般优于 `array_push`
    banxi1988
        13
    banxi1988  
    OP
       2016-01-27 09:27:21 +08:00
    @yadam 哈哈,让我知道了 array_combine 这个 函数.
    不过您的这个代码并不能达到想要的效果.
    你的代码输出结果:

    ```php
    (
    [1] => 种类 1
    [2] => 种类 2
    [3] => 种类 3
    [4] => 种类 4
    [5] => 种类 5
    )
    ```
    我需要的结果:

    ```php
    (
    [0] => Array
    (
    [id] => 1
    [name] => 种类 1
    )

    [1] => Array
    (
    [id] => 2
    [name] => 种类 2
    )

    [2] => Array
    (
    [id] => 3
    [name] => 种类 3
    )

    [3] => Array
    (
    [id] => 4
    [name] => 种类 4
    )

    [4] => Array
    (
    [id] => 5
    [name] => 种类 5
    )

    )
    ```
    anyforever
        14
    anyforever  
       2016-01-27 09:37:14 +08:00
    得看你数据来源,如果数据是死的,@xifangczy +1
    louk78
        15
    louk78  
       2016-01-27 10:55:56 +08:00
    第二张看起来优雅点
    changlers
        16
    changlers  
       2016-01-27 11:53:05 +08:00
    对于固定的数据而言,没啥区别吧,一开始一直用第二种,后来用第一种了
    chuhemiao
        17
    chuhemiao  
       2016-01-27 13:50:45 +08:00
    2 看起来简洁
    techmoe
        18
    techmoe  
       2016-01-27 15:55:10 +08:00 via Android
    array 套两层套进去就好了嘛

    这是我在这里的第一帖。。大家好我是新人,很高兴认识大家
    penjianfeng
        19
    penjianfeng  
       2016-01-27 21:52:56 +08:00
    肯定第二段,当然,什么写法不重要,都遵守同样的规则就行了
    libook
        20
    libook  
       2016-01-27 23:41:27 +08:00
    let cate_list = [
    {"id": 1, "name": "种类 1"},
    {"id": 2, "name": "种类 2"},
    {"id": 3, "name": "种类 3"},
    {"id": 4, "name": "种类 4"},
    {"id": 5, "name": "种类 5"}
    ]

    好吧,我是 JS 党混进来了~哈哈~可以无视我
    timsims
        21
    timsims  
       2016-01-28 15:08:44 +08:00
    如果是要在 View 里输出的话无论哪一种都不好
    jsjscool
        22
    jsjscool  
       2016-01-28 15:26:54 +08:00
    @banxi1988 `$cate[] = XX;` 这种写法非常不规范,而且你自己也能感觉到这种语法有坏的味道。
    yao978318542
        23
    yao978318542  
       2016-01-28 17:08:44 +08:00
    banxi1988
        24
    banxi1988  
    OP
       2016-01-30 13:03:03 +08:00
    @jsjscool
    我看 PHP 的文档 是这样写的:
    > Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
    jsjscool
        25
    jsjscool  
       2016-01-31 18:53:24 +08:00
    @banxi1988 函数调用肯定会有额外的开销。这就像使用 ORM , ORM 与原生的 SQL 相比效率相差不止一点,但是引入 ORM 对项目的管理,规范方面带来的好处远远多余那点执行效率。看你个人,你觉得用哪种更『美』就用哪种。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1692 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:14 · PVG 00:14 · LAX 09:14 · JFK 12:14
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.