PHP - JSON 编码输出仅包含 1 个 XML 条目而不是多个
PHP - JSON encode output contains only 1 XML entry instead of multiple
更新:
上一个post:
好的,我的代码对 1 位客户来说完美运行。现在我 运行 遇到另一个问题,如果我想接收多个客户作为 JSON 输出,我该怎么做?
我在状态为 C 的帐户下创建了第二个条目,并修改了我目前收到的代码部分,基本上从所有状态为 C 的帐户输出 name/email/...。但是我只是收到它看到的第一个。可能哪里弄错了
$document = new DOMDocument();
$document->loadXML($response);
$xpath = new DOMxpath($document);
$json = [];
foreach ($xpath->evaluate('(/eExact/Accounts)') as $account) {
$json['email'] = $xpath->evaluate('string(Account[@status="C"]/Email)', $account);
$json['first_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account);
$json['last_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account);
$json['billing'] = [
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'postcode'=> $xpath->evaluate('string(Account[@status="C"]/Address[@default="1"]/PostalCode)', $account),
];
// ...
}
$customer = json_encode($json, JSON_PRETTY_PRINT);
echo $customer;
输出:
{
"email": "example1@example1.com",
"first_name": "John",
"last_name": "Doe",
"billing": {
"first_name": "John",
"last_name": "Doe",
"postcode": "10000"
}
}[Finished in 0.4s]
我真正想要的是这样的:[或者换句话说,所有状态为 C 的帐户]
{
"email": "example1@example1.com",
"first_name": "John",
"last_name": "Doe",
"billing": {
"first_name": "John",
"last_name": "Doe",
"postcode": "10000"
}
{
"email": "example2@example2.com",
"first_name": "Steve",
"last_name": "Doe",
"billing": {
"first_name": "Steve",
"last_name": "Doe",
"postcode": "20000"
}
您将在每次迭代时覆盖 $json
数组键。您将其设为一维数组,而您需要的是多维数组(其中每个元素是另一个数组):
foreach ($xpath->evaluate('(/eExact/Accounts)') as $account) {
$json[] = [
'email' => $xpath->evaluate('string(Account[@status="C"]/Email)', $account),
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'billing' => [
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'postcode'=> $xpath->evaluate('string(Account[@status="C"]/Address[@default="1"]/PostalCode)', $account),
],
];
}
这里的关键是 $json[] = ...
- 这告诉 PHP 在数组中创建一个新元素并为其分配后面的值。
您正在创建一个数组并在每次循环中覆盖它。有多种追加方法,但使用现有代码,您可以执行如下操作:
foreach ($xpath->evaluate('(/eExact/Accounts)') as $account) {
$json['email'] = $xpath->evaluate('string(Account[@status="C"]/Email)', $account);
$json['first_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account);
$json['last_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account);
$json['billing'] = [
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'postcode'=> $xpath->evaluate('string(Account[@status="C"]/Address[@default="1"]/PostalCode)', $account),
];
// append to your result here
$result[] = $json;
}
$customer = json_encode($result, JSON_PRETTY_PRINT);
echo $customer;
更新:
上一个post:
好的,我的代码对 1 位客户来说完美运行。现在我 运行 遇到另一个问题,如果我想接收多个客户作为 JSON 输出,我该怎么做?
我在状态为 C 的帐户下创建了第二个条目,并修改了我目前收到的代码部分,基本上从所有状态为 C 的帐户输出 name/email/...。但是我只是收到它看到的第一个。可能哪里弄错了
$document = new DOMDocument();
$document->loadXML($response);
$xpath = new DOMxpath($document);
$json = [];
foreach ($xpath->evaluate('(/eExact/Accounts)') as $account) {
$json['email'] = $xpath->evaluate('string(Account[@status="C"]/Email)', $account);
$json['first_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account);
$json['last_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account);
$json['billing'] = [
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'postcode'=> $xpath->evaluate('string(Account[@status="C"]/Address[@default="1"]/PostalCode)', $account),
];
// ...
}
$customer = json_encode($json, JSON_PRETTY_PRINT);
echo $customer;
输出:
{
"email": "example1@example1.com",
"first_name": "John",
"last_name": "Doe",
"billing": {
"first_name": "John",
"last_name": "Doe",
"postcode": "10000"
}
}[Finished in 0.4s]
我真正想要的是这样的:[或者换句话说,所有状态为 C 的帐户]
{
"email": "example1@example1.com",
"first_name": "John",
"last_name": "Doe",
"billing": {
"first_name": "John",
"last_name": "Doe",
"postcode": "10000"
}
{
"email": "example2@example2.com",
"first_name": "Steve",
"last_name": "Doe",
"billing": {
"first_name": "Steve",
"last_name": "Doe",
"postcode": "20000"
}
您将在每次迭代时覆盖 $json
数组键。您将其设为一维数组,而您需要的是多维数组(其中每个元素是另一个数组):
foreach ($xpath->evaluate('(/eExact/Accounts)') as $account) {
$json[] = [
'email' => $xpath->evaluate('string(Account[@status="C"]/Email)', $account),
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'billing' => [
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'postcode'=> $xpath->evaluate('string(Account[@status="C"]/Address[@default="1"]/PostalCode)', $account),
],
];
}
这里的关键是 $json[] = ...
- 这告诉 PHP 在数组中创建一个新元素并为其分配后面的值。
您正在创建一个数组并在每次循环中覆盖它。有多种追加方法,但使用现有代码,您可以执行如下操作:
foreach ($xpath->evaluate('(/eExact/Accounts)') as $account) {
$json['email'] = $xpath->evaluate('string(Account[@status="C"]/Email)', $account);
$json['first_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account);
$json['last_name'] = $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account);
$json['billing'] = [
'first_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/FirstName)', $account),
'last_name' => $xpath->evaluate('string(Account[@status="C"]/Contact[@default="1"]/LastName)', $account),
'postcode'=> $xpath->evaluate('string(Account[@status="C"]/Address[@default="1"]/PostalCode)', $account),
];
// append to your result here
$result[] = $json;
}
$customer = json_encode($result, JSON_PRETTY_PRINT);
echo $customer;