首先值,壓平陣列(使用來自Standard PHP Library的RecursiveIteratorIterator和RecursiveArrayIterator(這對於很多事情來說非常方便),然後測試空白值:
$jfo = json_decode($json_file, true); // return an array, not an object
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($jfo));
foreach($it as $k => $v) {
if(!empty($v)) { // test to see if the value is NOT empty
echo $k . " value is " . $v . "<br />";
}
}
返回:
Count value is 115
Message value is Results returned successfully
SearchCriteria value is VIN(s): 1G1PE5S95B7254749
AirBagLocFront value is 1st Row (Driver & Passenger)
AirBagLocKnee value is 1st Row (Driver & Passenger)
AirBagLocSide value is 1st & 2nd Rows
BodyClass value is Sedan/Saloon
DisplacementCC value is 1400.0
DisplacementCI value is 85.43324173262
DisplacementL value is 1.4
Doors value is 4
EngineConfiguration value is In-Line
EngineCylinders value is 4
EngineModel value is LUJ
ErrorCode value is 0 - VIN decoded clean. Check Digit (9th position) is correct
FuelInjectionType value is Multipoint Fuel Injection (MPFI)
FuelTypePrimary value is Gasoline
Make value is CHEVROLET
Manufacturer value is GENERAL MOTORS LLC
ManufacturerId value is 984
Model value is Cruze
ModelYear value is 2011
OtherEngineInfo value is HO, ALUM GME
OtherRestraintSystemInfo value is Airbags: Roof Side - all seating rows
PlantCity value is Lordstown
PlantCompanyName value is GMNA
PlantCountry value is United States (USA)
PlantState value is Ohio
SeatBeltsAll value is Manual
TransmissionStyle value is Automatic
Trim value is LT
Turbo value is Yes
VIN value is 1G1PE5S95B7254749
ValveTrainDesign value is Dual Overhead Cam (DOHC)
VehicleType value is PASSENGER CAR
Windows value is 4
這仍然不能格式,我需要返回鍵和值。你能幫我嗎? – fredcampbell
添加第二個'true'參數'json_decode($ json_file,true)',然後執行foreach($ jfo ['Results']作爲$ item)'。這看起來像你想要的方式嗎? –
@PradyutManna告訴你的是,使用'true'標誌返回一個數組,而不是一個對象。該數組將是多維的,所以您將不得不使用遞歸函數來處理每個鍵/值對,測試該值是否爲空,然後將該鍵添加到列表中。 –