哪一种数据结构比较好? [英] Which kind of data structure is better?

查看:153
本文介绍了哪一种数据结构比较好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了我的网站的API。该API将其他开发者或Android应用程序中使用。嗯,我有两种数据结构:

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:

一:

$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"
         },
    }
} */

二:

$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
             ],
     }
} */

那么,哪些数据结构是在客户端使用它更好呢?的(其他网站,移动应用)

推荐答案

在你模拟使用数组实际上并没有使用数组的第一个(或至少它看起来像给你的输出),并在第二一个你正在重复信息,并迫使开发商把它放在一起。最好的解决办法是:

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: ""
        }
    ]
}

我认为这是类似于您的第一选择pretty,但看起来像插件显示一个糟糕的方​​式什么的吧。

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

主要理由Android的开发者preFER这是因为对象和JSON之间的直接转换可以用一些库来完成。

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

这篇关于哪一种数据结构比较好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆