如何从php制作这个json格式?

时间:2013-12-27 20:04:15

标签: php mysql json

嗨,所有开发者

我有以下json格式

[
  {
    "name": "typeahead.js",
    "description": "A fast and fully-featured autocomplete library",
    "language": "JavaScript",
    "value": "typeahead.js",
    "tokens": [
      "typeahead.js",
      "JavaScript"
    ]
  },
  {
    "name": "cassandra",
    "description": "A Ruby client for the Cassandra distributed database",
    "language": "Ruby",
    "value": "cassandra",
    "tokens": [
      "cassandra",
      "Ruby"
    ]
  },
]

我用这些列

创建了一个名为 testable 的mysql表

ID,名称,说明,语言,值,令牌

我试图通过 php testable 生成上述格式的json文件。

你能否就此提出建议?

4 个答案:

答案 0 :(得分:2)

对于此示例,我假设您使用MySQLi。我还假设您将令牌存储为单独的值,因为您没有提及有关第二个表的任何内容。

$db = new Mysqli(<your credentials>);
$query = $db->query("SELECT * FROM testable");

$arr = array();
while($row = $query->fetch_assoc()){
    $tokens = explode('<separator>', $row['tokens']);
    $row['tokens'] = $tokens;

    $arr[] = $row;
}

echo json_encode($arr);

答案 1 :(得分:1)

这样的事情:

define('DB_HOST', 'Your db host here');
define('DB_NAME', 'Your db name here');
define('DB_USER', 'Your db user here');
define('DB_PASSWORD', 'Your db password here');
$connectionString = 'mysql:host=' . DB_HOST . ';database=' . DB_NAME;
$pdo = new PDO($connectionString, DB_USER, DB_PASSWORD);
$sql = "SELECT name, description, language, value, tokens FROM testable";
// Prepared statement now b/c it's a better default.
$stmt = $pdo->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetchObject())
{
    $results[] = $row;
    $results[count($results) - 1]->tokens = blackMagic($row->tokens); //Need to see how tokens are stored to do the blackMagic function.
}
echo json_encode($results);

答案 2 :(得分:0)

我们需要形成一个数组,以获得上述JSON格式。

数组如下:

<?php
  $array = array (
             0 => array (
                    'name' => 'typeahead.js',
                    'description' => 'A fast and fully-featured autocomplete library',
                    'language' => 'JavaScript',
                    'value' => 'typeahead.js',
                    'tokens' => array (
                                   0 => 'typeahead.js',
                                   1 => 'JavaScript',
                                ),
                  ),
             1 => array (
                    'name' => 'cassandra',
                    'description' => 'A Ruby client for the Cassandra distributed database',
                    'language' => 'Ruby',
                    'value' => 'cassandra',
                    'tokens' => array (
                                   0 => 'cassandra',
                                   1 => 'Ruby',
                                ),
                     ),
      ); 
  echo json_encode($array);
?>

答案 3 :(得分:0)

// fetch data from the table
$query = $dbcon->query("SELECT * FROM testable");

// form an array 
while($row = $query->fetch_assoc()){
    $arr = array();
    $arr["name"] = $row["name"];
    $arr["description"] = $row["description"];
    $arr["language"] = $row["language"];
    //... add more data here according to your records...


    $data[] = $arr;
}


// $data will be formed as :
/*    
Array
(
    [0] => Array
        (
            [name] => typeahead.js
            [description] => A fast and fully-featured autocomplete library
            [language] => JavaScript
            [value] => typeahead.js
            [tokens] => Array
                (
                    [0] => typeahead.js
                    [1] => JavaScript
                )

        )

    [1] => Array
        (
            [name] => cassandra
            [description] => A Ruby client for the Cassandra distributed database
            [language] => Ruby
            [value] => cassandra
            [tokens] => Array
                (
                    [0] => cassandra
                    [1] => Ruby
                )

        )

)

*/  

// then 
echo json_encode($data);


// output

[
    {
        "name":"typeahead.js",
        "description":"A fast and fully-featured autocomplete library",
        "language":"JavaScript",
        "value":"typeahead.js",
        "tokens":["typeahead.js","JavaScript"]
    },

    {
        "name":"cassandra",
        "description":"A Ruby client for the Cassandra distributed database",
        "language":"Ruby",
        "value":"cassandra",
        "tokens":["cassandra","Ruby"]
    }
]