array 배열 문의드립니다.

array 배열 문의드립니다.

QA

array 배열 문의드립니다.

본문

    "parcelInfoList": [{

            "amount": 0.1, # 상품 개당 가격

            "name": "iPhone", # 상품명

            "originCountry": "SG",  #상품 원산지

            "quantity": 1, #상품 개수

            "unit": "EA" #상품 단위

        }

    ],

 

위 배열에 [] <- 형님들~~ 이 기호를 어떻게 감쌀수가 있을까요?

 

예시)

제품명 선언 요소, 다중 요소 지원, "[]" 기호를 사용한 요소 이름, ";" 기호를 사용하여 분할하는 다중 요소, 입력 형식: "[{"brand":"test "},{"specifications":"test2"},{"mode":"L3"},{"Manufacturer Identification Code":"test2"}]"

이 질문에 댓글 쓰기 :

답변 1

"parcelInfoList": [
  {
    "amount": 0.1,
    "name": "iPhone",
    "originCountry": "SG",
    "quantity": 1,
    "unit": "EA",
    "productDetails": [{ "brand": "Apple" },{ "specifications": "128GB" },{ "mode": "Pro" },{ "Manufacturer Identification Code": "A1234" }
    ]
  }
]

or

$productDetails = "brand:Apple;specifications:128GB;mode:Pro;Manufacturer Identification Code:A1234";

$items = explode(";", $productDetails);
$result = [];

foreach ($items as $item) {
    list($key, $value) = explode(":", $item, 2);
    $result[] = [trim($key) => trim($value)];
}

echo json_encode($result, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);

 

답변을 작성하시기 전에 로그인 해주세요.
전체 129,672 | RSS
QA 내용 검색

회원로그인

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