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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条