Which kind of data structure is better?

时间:2015-12-14 18:13:25

标签: php android arrays data-structures

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 个答案:

答案 0 :(得分:1)

在第一个模拟数组的使用而不实际使用数组(或者至少它看起来像给出你的输出),在第二个你重复信息并强迫开发人员将它们放在一起。最好的解决方案是:

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

我认为这与你的第一个选项非常相似,但看起来插件显示的方式很糟糕。

Android开发人员更喜欢这样做的主要原因是因为对象和json之间的直接转换可以通过一些库完成。