-1

我在 PHP 方面没有经验,所以我需要你的帮助。如果我可以这样说,我想从两个数组中创建。基本上,我想以此为基础:

[{"Naziv":"Nemanja","duzina":"45.475","sirina":"23.423"},{"Naziv":"Aleksandar","duzina":"45.427","sirina":"21.124 "}]

像这样:

[{"Naziv":"Nemanja","duzina":["45.475","23.423"]},{"Naziv":"Aleksandar","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);
4

1 回答 1

1

代替

$rows[] = $row; 

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

$rows[] = array(
    'Naziv'  => $row['Naziv'],
    'duzina' => array($row['duzina'], $row['sirina'])
);
于 2013-08-26T10:23:34.900 回答