dongyou7739 2015-12-14 18:13
浏览 42
已采纳

哪种数据结构更好? [关闭]

I'm creating an API for my website. That API will be used by other developers or an Android application. Well, I have two kind of data structures:

One:

$iterator = 0;
while (($end = $sth->fetch()) && $iterator < 2)
{
    $api_arr["data"][] =  array(
        "id"      =>  $end["id"],
        "title"   =>  $end["title"],
        "content" =>  $end["content"] );   
$iterator ++;
}


/* Output: ------------ print_r($api_arr); -------------------

Array
(
   [data] => Array
      (
        [0] =>
          (
            [id]      => value_id_1
            [title]   => value_title_1
            [content] => value_content_1
          )

        [1] =>
          (
            [id]      => value_id_2
            [title]   => value_title_2
            [content] => value_content_2
          )
      )
) */


/* Output: ------------ echo json_encode($data); -------------------
             ----- and using JSONveiw extension on chrome -----

{
   - data: {
       - 0: {
           id: "value_id_1",
           title: "value_title_1",
           content: "value_content_1"
         },

       - 1: {
           id: "value_id_2",
           title: "value_title_2",
           content: "value_content_2"
         },
    }
} */

Two:

$iterator = 0;
while (($end = $sth->fetch()) && $iterator < 2)
{
    $api_arr["data"]['id'][]       =  $end["id"];
    $api_arr["data"]['title'][]    =  $end["title"];
    $api_arr["data"]['content'][]  =  $end["content"];
$iterator ++;
}


/* Output: ------------ print_r($api_arr); -------------------

Array
(
  [data] =>
     (
        [id] => Array
           (
               [0] => value_id_1
               [1] => value_id_2
               [2] => value_id_3
           ),

        [title] => Array
           (
               [0] => value_title_1
               [1] => value_title_2
               [2] => value_title_3
           ),

        [content] => Array
           (
               [0] => value_content_1
               [1] => value_content_2
               [2] => value_content_3
           )
      )
) */

/* Output: ------------ echo json_encode($data); -------------------
             ----- and using JSONveiw extension on chrome -----

{
   - data: {
        - id:[
               "value_id_1",
               "value_id_2",
               "value_id_3
             ],
        - title:[
               "value_title_1,
               "value_title_2,
               "value_title_3,
             ],
        - content:[
               "value_content_1",
               "value_content_2",
               "value_content_3
             ],
     }
} */

So, Which data-structure is better for using it in client side? (other websites, mobile-app)

  • 写回答

1条回答 默认 最新

  • dougan1884 2015-12-14 18:23
    关注

    In the first one you are simulating the use of an array without actually using an array (or at least it looks like given your output), and in the second one you are repeating info and forcing the developer to put it all together. The best solution is:

    {
        data: [
            {
                id: "",
                title: "",
                content: ""
            },
            {
                id: "",
                title: "",
                content: ""
            }
        ]
    }
    

    I think this is pretty similar to your first option, but looks like the plugin is showing it in a bad way or something.

    The main reason why Android devs prefer that is because a direct translation between objects and json can be done with some libraries.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器