I have a long list of nested foreach loops that does what I want from an output standpoint. However I want to write a function that can do this looping for me. Here is the XML file I am looping through, the code so far and the output.
The XML test.xml
[php]<?xml version="1.0" encoding="UTF-8"?>
<successful_payment_notification>
<account_code>admin</account_code>
admin
[email protected]
<first_name>Super_test</first_name>
<last_name>Admin_test</last_name>
<company_name nil=“true”></company_name>
32cf2700fd814e05ed9cc949279844b3
<invoice_id>32cf2700f0b3f88b7644324214bd2fb3</invoice_id>
<invoice_number_prefix></invoice_number_prefix>
<invoice_number type=“integer”>1098</invoice_number>
<subscription_id>32cf2700ab0246a6b14e244bae8cb6b4</subscription_id>
purchase
2015-12-02T01:43:24Z
<amount_in_cents type=“integer”>6490</amount_in_cents>
success
Successful test transaction
6914228
subscription
<cvv_result code=""></cvv_result>
<avs_result code=“D”>Street address and postal code match.</avs_result>
<avs_result_street nil=“true”></avs_result_street>
<avs_result_postal nil=“true”></avs_result_postal>
true
true
true
</successful_payment_notification>
[/php]
The code test.php
[php]<?php
echo “
”;”;
require_once ‘…/xml2array/xml2array.php’;$doc = new DOMDocument();
$doc->load(‘test.xml’);$array = XML2Array::createArray($doc);
function foreachloop ($arr, $keys) {
$return_array = array();
foreach ($arr as $key => $value) {
if (is_array($value)) {
$keys .= $key."_";
foreachloop($value, $keys);
} else {
echo $keys.$key." = “.$value.”
“;
}
}
}
foreachloop ($array, $keys);
print_r ($return_array);
#print_r ($array);
echo “
”;
foreach ($array as $key => $value) {
echo $key.”
";
$user = user_load_by_name($value[‘account’][‘account_code’]);
$values = array(
‘uid’ => $user->uid,
);foreachloop ($value, $keys);
echo "<br/>"; echo $key."<br/>"; foreach ($value as $ke => $va) { if (is_array($va)) { foreach ($va as $k => $v) { if (is_array($v)) { foreach ($v as $a => $b) { if (is_array($b)) { foreach ($b as $c => $d) { if (is_array($d)) { foreach ($d as $e => $f) { if (is_array($f)) { foreach ($f as $g => $h) { if (is_array($h)) { foreach ($h as $i => $j) { echo $ke."_".$k."_".$a."_".$c."_".$e."_".$g."_".$i." = ".$j."<br/>"; } } else { echo $ke."_".$k."_".$a."_".$c."_".$e."_".$g." = ".$h."<br/>"; } } } else { echo $ke."_".$k."_".$a."_".$c."_".$e." = ".$f."<br/>"; } } } else { echo $ke."_".$k."_".$a."_".$c." = ".$d."<br/>"; } } } else { echo $ke."_".$k."_".$a." = ".$b."<br/>"; } } } else { echo $ke."_".$k." = ".$v."<br/>"; } } } else { echo $ke." = ".$va."<br/>"; } }
}
error_log(‘Parse Attempted’);
echo “
?>[/php]
The output so far, the top is not formatted correctly the bottom is.
[php]successful_payment_notification_account_account_code = admin
successful_payment_notification_account_username = admin
successful_payment_notification_account_email = [email protected]
successful_payment_notification_account_first_name = Super_test
successful_payment_notification_account_last_name = Admin_test
successful_payment_notification_account_company_name_@value =
successful_payment_notification_account_company_name_@attributes_nil = true
successful_payment_notification_account_transaction_id = 32cf2700fd814e05ed9cc949279844b3
successful_payment_notification_account_transaction_invoice_id = 32cf2700f0b3f88b7644324214bd2fb3
successful_payment_notification_account_transaction_invoice_number_prefix =
successful_payment_notification_account_transaction_invoice_number_@value = 1098
successful_payment_notification_account_transaction_invoice_number_@attributes_type = integer
successful_payment_notification_account_transaction_invoice_number_subscription_id = 32cf2700ab0246a6b14e244bae8cb6b4
successful_payment_notification_account_transaction_invoice_number_action = purchase
successful_payment_notification_account_transaction_invoice_number_date_@value = 2015-12-02T01:43:24Z
successful_payment_notification_account_transaction_invoice_number_date_@attributes_type = datetime
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_@value = 6490
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_@attributes_type = integer
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_status = success
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_message = Successful test transaction
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_reference = 6914228
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_source = subscription
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_@value =
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_@attributes_code =
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_@value = Street address and postal code match.
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_@attributes_code = D
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_@value =
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_@attributes_nil = true
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_@value =
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_@attributes_nil = true
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_test_@value = true
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_test_@attributes_type = boolean
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_test_voidable_@value = true
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_test_voidable_@attributes_type = boolean
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_test_voidable_refundable_@value = true
successful_payment_notification_account_transaction_invoice_number_date_amount_in_cents_cvv_result_avs_result_avs_result_street_avs_result_postal_test_voidable_refundable_@attributes_type = boolean
successful_payment_notification
account_account_code = admin
account_username = admin
account_email = [email protected]
account_first_name = Super_test
account_last_name = Admin_test
account_company_name_@value =
account_company_name_@attributes_nil = true
transaction_id = 32cf2700fd814e05ed9cc949279844b3
transaction_invoice_id = 32cf2700f0b3f88b7644324214bd2fb3
transaction_invoice_number_prefix =
transaction_invoice_number_@value = 1098
transaction_invoice_number_@attributes_type = integer
transaction_subscription_id = 32cf2700ab0246a6b14e244bae8cb6b4
transaction_action = purchase
transaction_date_@value = 2015-12-02T01:43:24Z
transaction_date_@attributes_type = datetime
transaction_amount_in_cents_@value = 6490
transaction_amount_in_cents_@attributes_type = integer
transaction_status = success
transaction_message = Successful test transaction
transaction_reference = 6914228
transaction_source = subscription
transaction_cvv_result_@value =
transaction_cvv_result_@attributes_code =
transaction_avs_result_@value = Street address and postal code match.
transaction_avs_result_@attributes_code = D
transaction_avs_result_street_@value =
transaction_avs_result_street_@attributes_nil = true
transaction_avs_result_postal_@value =
transaction_avs_result_postal_@attributes_nil = true
transaction_test_@value = true
transaction_test_@attributes_type = boolean
transaction_voidable_@value = true
transaction_voidable_@attributes_type = boolean
transaction_refundable_@value = true
transaction_refundable_@attributes_type = boolean[/php]