현재 타사 API를 사용하고 있습니다. json 객체에 db 값을 형식화하는 일부 오류가 있습니다. API는 특정 형식 인 경우 값을 가져옵니다. 아래의 PHP 코드에서 foreach 루프를 사용하여 결과를 가져옵니다. 그런 다음 array_push
을 입력하여 배열 내부에 값을 배치하십시오. coordinates
에 대한 숫자 대신 문자열 데이터가 나타납니다. 결국, 나는 원하는 결과를 얻지 못하고있다. 아래 예제와 같이 json 객체를 어떻게 포맷 할 수 있습니까?php json json 객체 용 인코딩 형식
현재 결과 :
{
coordinates: [
"-121.2808, 38.3320"
],
attributes: "Sacramento"
},
원하는 결과 :
{
coordinates: [-121.2808, 38.3320],
attributes: {
name: 'Sacramento'
}
},
PHP의 루프로 json_encode
header("Content-type: application/json");
//get the course list
$location_query = $db_con->prepare("SELECT city, X(location) as latitude, Y(location) as longitude
FROM company
");
$location_query->execute();
$data = $location_query->fetchAll();
$output = array();
foreach ($data as $row) {
array_push($output, array('coordinates'=>array($row["latitude"].",".$row["longitude"]), 'attributes'=>$row["city"]));
}// foreach ($data as $row) {
echo json_encode($output);
당신은 그 문자열의 부동 소수점 표현을 얻을 필요가
닫기에 너무 집중했다. ' 'attributes'=> array ('name'=> $ row [ "city"]))' – Mike
@ 감사합니다! :) – SamV
고마워요! 이제는 의미가 있습니다. 나는 float을 사용할 것이다. – techAddict82