You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将XML单标签类数组及vehicleJson内JSON存储到PHP变量

Hey there! Let's break down your two PHP questions about handling that XML data, step by step.

1. Storing XML's nested (array-like) data into PHP variables

First, since your XML uses a default namespace (http://regcheck.org.uk), we need to account for that when parsing. We'll use PHP's built-in SimpleXMLElement class to handle the XML smoothly:

// Your raw XML string
$xmlString = '<Vehicle xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://regcheck.org.uk"> <vehicleJson> { "Description": "JCB INDIA LIMITED / JCB 3DX", "RegistrationYear": "2010", "CarMake": { "CurrentTextValue": "JCB INDIA LIMITED" }, "CarModel": { "CurrentTextValue": "JCB 3DX" }, "MakeDescription": { "CurrentTextValue": "JCB INDIA LIMITED" }, "ModelDescription": { "CurrentTextValue": "JCB 3DX" }, "VechileIdentificationNumber": "1710000", "EngineNumber": "XXXXX", "FuelType": { "CurrentTextValue": "DIESEL" }, "RegistrationDate": "13/08/2010", "Owner": "RAMDEBHAI KARANGIYA", "Fitness": "18-Sep-2019", "Insurance": "05-Nov-2019", "Location": "RTO,PORBANDAR HIGHWAY", "ImageUrl": "http://in.carregistrationapi.com/image.aspx/@SkNCIElORElBIExJTUlURUQgLyBKQ0IgM0RY" } </vehicleJson> <vehicleData> <Description>JCB INDIA LIMITED / JCB 3DX</Description> <RegistrationYear>2010</RegistrationYear> <CarMake> <CurrentTextValue>JCB INDIA LIMITED</CurrentTextValue> </CarMake> <CarModel>JCB 3DX</CarModel> </vehicleData> </Vehicle>';

// Parse the XML string into a SimpleXMLElement object
$xml = new SimpleXMLElement($xmlString);

// Access the default namespace to target the correct nodes
$ns = $xml->children('http://regcheck.org.uk');

// Grab the <vehicleData> node
$vehicleData = $ns->vehicleData;

// Extract values into PHP variables (cast to string to get raw text, not object)
$description = (string)$vehicleData->Description;
$registrationYear = (string)$vehicleData->RegistrationYear;
$carMake = (string)$vehicleData->CarMake->CurrentTextValue;
$carModel = (string)$vehicleData->CarModel;

// Optional: Verify the variables
echo "Description: $description \n";
echo "Registration Year: $registrationYear \n";
echo "Car Make: $carMake \n";
echo "Car Model: $carModel \n";

The critical bits here are targeting the default namespace with children() and casting each node to a string—this pulls out the actual text value instead of returning a SimpleXMLElement object.

2. Extracting specific fields from the JSON inside tag

The <vehicleJson> tag holds a valid JSON string, so we'll extract that string first, decode it into a PHP array/object, then pull out the fields you need:

// Get the raw JSON string from the <vehicleJson> node
$vehicleJsonStr = (string)$ns->vehicleJson;

// Decode JSON into an associative array (use false instead of true for an object)
$vehicleJson = json_decode($vehicleJsonStr, true);

// Add error handling in case JSON parsing fails
if (json_last_error() !== JSON_ERROR_NONE) {
    die('Failed to parse JSON: ' . json_last_error_msg());
}

// Extract specific fields into individual PHP variables
$jsonDescription = $vehicleJson['Description'];
$jsonCarMake = $vehicleJson['CarMake']['CurrentTextValue'];
$owner = $vehicleJson['Owner'];
$fuelType = $vehicleJson['FuelType']['CurrentTextValue'];
$registrationDate = $vehicleJson['RegistrationDate'];

// Optional: Verify the variables
echo "\nFrom JSON Data:\n";
echo "Description: $jsonDescription \n";
echo "Car Make: $jsonCarMake \n";
echo "Owner: $owner \n";
echo "Fuel Type: $fuelType \n";

Using true in json_decode() converts the JSON into an associative array, making it easy to access nested values like CarMake->CurrentTextValue via array keys. If you prefer working with objects, just omit the second parameter (or pass false), then access values like $vehicleJson->CarMake->CurrentTextValue.


内容的提问来源于stack exchange,提问作者Jigar Karangiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:58:00