mysql data 가공에 대해 질문드립니다.

매출이 오르면 내리는 수수료! 지금 수수료센터에서 전자결제(PG)수수료 비교견적 신청해 보세요!
mysql data 가공에 대해 질문드립니다.

QA

mysql data 가공에 대해 질문드립니다.

본문

이곳에 질문하는게 맞는지 모르겠지만;;

 

 


[
  {id: 1, store: '우리동네 카페', drink: '아메리카노', price: 2500},
  {id: 1, store: '우리동네 카페', drink: '카페라떼', price: 3500},
  {id: 1, store: '우리동네 카페', drink: '수박주스', price: 4500},
  {id: 2, store: '이디야', drink: '아메리카노', price: 1500},
  {id: 2, store: '이디야', drink: '핫초코', price: 2500}
]

 

select join 문 사용하여 위와 같은 형태의 데이터를 받아왔다고 쳤을때

받아온 데이터를 아래의 형태로 가공하고자 하려면 어떻게 해야 하는지요...

 


[
  {
    id: 1,
    store: '우리동네 카페',
    storeInfo: [
      { drink: '아메리카노', price: 2500 },
      { drink: '카페라떼', price: 3500 },
      { drink: '수박주스', price: 4500 }
    ]
  },
  {
    id: 2,
    store: '이디야',
    storeInfo: [
      { drink: '아메리카노', price: 1500 },
      { drink: '핫초코', price: 2500 }
    ]
  }
]

 

감사합니다~

이 질문에 댓글 쓰기 :

답변 1

오류검사는 하지 않았습니다.

 


$result = mysqli_query(" select * from tbl");
while($row=mysqli_fetch_assoc($result)){
   if(!isset($data[$row['id']]['id']))
        $data[$row['id']]['id'] = $row['id'];
   if(!isset($data[$row['id']]['store']))
           $data[$row['id']]['store'] = $row['store'];
   $storeInfo['drink'] = $row['drink'];
   $storeInfo['price'] = $row['price'];
   
   $data[$row['id']]['storeInfo'][] = $storeInfo;    
}
foreach($data as $id=>$array )
    $return[] = $array;
var_dump($return);
답변을 작성하시기 전에 로그인 해주세요.
전체 0
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT