我是php的新手,尝试在php中创建一个嵌套的json数组,但没有获得所需的json格式。
所需格式
{
"offers": [
{
"offerId": "1",
"offerName": "shirts",
"price": "1980",
"storeName": "peter england"
},
{
"offerId": "2",
"offerName": "shirts",
"storeId": "3",
"price": "2970",
"storeName": "peter england"
}
],
"brands": [
{
"brandName": "peter england",
"brandId": "5"
},
{
"brandName": "ruggers",
"brandId": "4"
}
],
"stores": [
{
"storeName": "peter england",
"storeId": "3"
},
{
"storeName": "peter england",
"storeId": "4"
}
],
"filters": [
{
"0": {
"filter": "M",
"filterId": "11"
},
"1": {
"filter": "S",
"filterId": "12"
},
"filterName": "size"
},
{
"0": {
"filter": "red",
"filterId": "13"
},
"1": {
"filter": "blue",
"filterId": "14"
},
"filterName": "colour"
}
]
}
但获得此格式
{
"offers": [
{
"offerId": "1",
"offerName": "shirts",
"image": "http://www.offersmashup.com/testing/store/upload/sml_p1.jpg",
"brandId": "5",
"storeId": "3",
"price": "1980",
"offer": "Off-20",
"storeName": "peter england"
},
{
"offerId": "2",
"offerName": "shirts",
"image": "http://www.offersmashup.com/testing/store/upload/sml_p2.jpg",
"brandId": "5",
"storeId": "3",
"price": "2970",
"offer": "Off-30",
"storeName": "peter england"
}
],
"brands": [
{
"brandName": "peter england",
"brandId": "5"
},
{
"brandName": "ruggers",
"brandId": "4"
}
],
"stores": [
{
"storeName": "peter england",
"storeId": "3"
},
{
"storeName": "peter england",
"storeId": "4"
}
],
"filters": {
"0": {
"filter": "M",
"filterId": "11"
},
"1": {
"filter": "S",
"filterId": "12"
},
"filterName": "size"
}
}
我的php代码在
之下$dbFactory = new DBFactory();
$jsonArray = array();
if ($subcatid != '')
{
$p2 = $dbFactory->sel_product3($subcatid, $area_value);
if (count($p2) > 0)
{
for ($j = 0; $j < count($p2); $j++)
{
$p3 = $dbFactory->sel_store($p2[$j]['STORE_ID']);
$temp['offers'][] = array(
"offerId" => $p2[$j]['P_ID'],
"offerName" => $p2[$j]['P_NAME'],
"image" => $url . $p2[$j]['P_ID'] . '.jpg',
"brandId" => $p2[$j]['BRAND_ID'],
"storeId" => $p2[$j]['STORE_ID'],
"price" => $p2[$j]['PRICE'],
"offer" => $p2[$j]['OFFER'],
"storeName" => $p3[0]['S_NAME']
);
$jsonArray = $temp;
}
}
}
// filters code=================
/*code to retrive brnads and stores goes here*/
foreach($res as $value)
{
$br = $dbFactory->sel_brand($value);
$temp["brands"][] = array("brandName" => $br[0]['BRAND_NAME'] ,"brandId"=>$br[0]['BID'] );
$jsonArray = $temp;
}
foreach($store_value as $value)
{
$sr = $dbFactory->sel_store($value);
$temp["stores"][] = array("storeName" => $sr[0][S_NAME] ,"storeId"=>$sr[0][S_ID] );
$jsonArray = $temp;
}
$parentCatId = $dbFactory->getparentcatId($subcatid);
$fg = $dbFactory->get_filtergroup($parentCatId[0]['PARENT_ID']);
for ($i = 0; $i < count($fg); $i++)
{
$f = $dbFactory->sel_filter($fg[$i][F_ID]);
$temp['filters'] = array();
$temp['filters']['filterName'] = $fg[$i][FNAME];
for ($j = 0; $j < count($f); $j++)
{
$temp['filters'][] = array("filter"=>$f[$j][FNAME],"filterId"=>$f[$j][F_ID] );
}
array_push($jsonArray, $temp);
}
$jsonArray = $temp;
echo json_encode($jsonArray);
尝试了很多以获得所需的json格式但是失败了。请帮助我解决这个问题