1
如果沒有數據存在,如何將數組值設置爲null
?PHP數組設置空值
以下是我的PHP陣列和我JSON編碼 -
{
"title":"Impalz-Marketing",
"type":"Business Details",
"version":"1.0",
"login":[ ],
"business":{
"1":{
"details":{ },
"messages":[ ],
"offers":[ ],
"events":[ ],
"milestone":[ ],
"products":[ ],
"brand_exp":[ ],
"reviews":[ ],
"agg_reviews":{ }
},
"168":{
"details":{ },
"messages":[ ],
"products":[ ]
}
}
}
行數是在商業不均勻。如果不存在行,如何將數據設置爲null?
$data = array(
'title' => 'Impalz-Marketing',
'type' => 'Business Details',
'version' => '1.0',
'login' => $login_array,
'business' => $business_details_array
);
我已經試過這樣:
$business_details_array = array();
while($row = mysql_fetch_assoc($biz_list))
{
$business_details_array[$row['id']]['details'] = $row;
}
while($row = mysql_fetch_assoc($biz_milestone))
{
if(!empty($row['id'])){
$temp= explode(',', $row['path']);
if(count($temp) > 1) {
$row['path']= $temp;
}
$business_details_array[$row['business_id']]['milestone'][] = $row;
}else{
$business_details_array[]['milestone'][] = null; // since no data exist their wont be any business_id
}
}
我想是這樣的 -
{
"title":"Impalz-Marketing",
"type":"Business Details",
"version":"1.0",
"login":[ ],
"business":{
"1":{
"details":{ },
"messages":[ ],
"offers":[ ],
"events":[ ],
"milestone":[ ],
"products":[ ],
"brand_exp":[ ],
"reviews":[ ],
"agg_reviews":{ }
},
"168":{
"details":{ },
"messages":[ ],
"offers": "null",
"events": "null",
"milestone": "null",
"products":[ ],
"brand_exp": "null",
"reviews": "null",
"agg_reviews": "null"
}
}
}
你可以發佈更多的代碼來創建數組。看起來你可能需要在while循環之前加上= null位。您應該將商業標識存儲在某個地方。 $ business_details_array [$ thisBusinessId] ['milestone'] [] = null; – Brett
我已經添加了代碼,在這個代碼中我使用'business_id'獲取所有業務列表,並且根據它進一步過濾詳細信息。 –