一个字段中的两个字段

时间:2013-08-26 10:20:57

标签: php

我没有PHP经验,所以我需要你的帮助。我想在两个阵列上创建,如果我可以这样说的话。 基本上,我想从中做出:

  

[{ “Naziv”: “维迪奇”, “duzina”: “45.475”, “Sirina酒店”: “23.423”},{ “Naziv”: “亚历山大”, “duzina”: “45.427”, “Sirina酒店” : “21.124”}]

这样的事情:

  

[{ “Naziv”: “维迪奇”, “duzina”:[ “45.475”, “23.423”]},{ “Naziv”: “亚历山大”, “duzina”:[ “45.427”, “21.124”] }]

这是我的PHP:

// Create connection
$con = mysqli_connect($host, $user, $pwd, $db);

// Check connection
if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 

// query the application data

$sql = "SELECT Naziv,duzina,sirina FROM lokacije";
$result = mysqli_query($con, $sql);

// an array to save the application data
$rows = array();

// iterate to query result and add every rows into array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

$rows[] = $row; 

}

// close the database connection
mysqli_close($con);

// echo the application data in json format
echo json_encode($rows);

1 个答案:

答案 0 :(得分:1)

而不是

$rows[] = $row; 

以您想要的格式创建一个新数组:

$rows[] = array(
    'Naziv'  => $row['Naziv'],
    'duzina' => array($row['duzina'], $row['sirina'])
);