내 가게의 제품이 PHP를 사용하지만 운이없는이 json 파일을 구문 분석하려고합니다! 나는 PHP를 처음 접했고 온라인 검색을 시도했지만 대답이 없었습니다. 다음은 내 json 파일의 일부입니다.
[
{
"_type": "default",
"description": [
"Product Description comes here"
],
"url": "here is the url to my seo friendly url",
"price": [
"11.64"
],
"sizes": [
"sizes come here"
],
"images": [
"image name and address is here"
],
"_template": "5c1333451e128f546e451197f6c6061b3a66d1ee",
"_cached_page_id": "5119b553b1512ae1a19d6cd0d3f29e51e111a90c",
"name": [
"product name is here"
]
},
{
"_type": "default",
"description": [
"Product #2 Description comes here"
],
"url": "here is the url to my seo friendly url",
"price": [
"11.64"
],
"sizes": [
"sizes come here"
],
"images": [
"image name and address is here"
],
"_template": "5c1333451e128f546e451197f6c6061b3a66d1ee",
"_cached_page_id": "5119b553b1512ae1a19d6cd0d3f29e51e111a90c",
"name": [
"product #2 name is here"
]
}
]
이것은 전체 json 파일의 샘플이지만이 형식을 따릅니다. 나는 그것을 색인하기 위해 다음 PHP 코드를 작성하려고 시도했지만 내가 말했듯이 운이 없다.
<?php
// this code reads from the files provided to it and populates the database with products
$jsondata = file_get_contents("55products.json");
$json = json_decode($jsondata, true);
foreach ($json["products"] as $product)
{
// now lets split it into smaller pieces
foreach ($product as $product_infos) {
print_r($product_infos);
echo "<br /><br /><br />";
}
}
하지만 여전히 콘텐츠에 액세스 할 수 없거나 구체적으로 말하자면 거기에있는 데이터 저장소에 액세스하는 방법을 모릅니다. 나는 이것이 매우 어리석은 질문이라는 것을 이해하지만 온라인의 모든 자습서에는 비슷한 구문이 없으므로 나를 도와주세요. 친애하는.
내부 요소는 배열 자체이므로 인덱스 0을 사용하여 값에 액세스해야합니다.
$obj = json_decode($json,true);
// var_dump($obj);exit;
foreach ($obj as $product)
{
print $product["description"][0] . "<br/>";
print $product["images"][0] . "<br/>";
print $product["url"] . "<br/>";
print $product["name"][0] . "<br/>";
}
출력됩니다
Product Description comes here
image name and address is here
here is the url to my seo friendly url
product name is here
Product #2 Description comes here
image name and address is here
here is the url to my seo friendly url
product #2 name is here
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다