Help with Nested Foreach Loops

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]

Something very not right here. You should look into SimpleXML Parser

I am familiar with SimpleXML parser and it does not contain the functionality that I need, I have to receive a Webhook from a vender in XML format and then parse that information on the fly. Simple XML is great if you have a file, it is not so good if you have to receive a stream of data. Besides my handling of the XML file is not part of the question.

Besides which my handling of the XML file is not part of the question.

A good programmer would want the right/best way to handle a given situation. What your doing is clearly neither the right or best way. Since you have no interest in doing things right I will leave it to someone else.

Well, Andrew, Kevin is correct. You are attempting to parse an XML file no matter how it is stored.
There are hundreds of XML parsers out there that can do this job for you in the, well, “correct” way.

The problem with your method is that it over-taxes the server for something that can be done with built-in
PHP functions or a solid library such as SimpleXML which is really part of PHP now. Here are a couple of links
for you to read up on.

http://www.w3schools.com/php/php_xml_simplexml_read.asp
http://php.net/manual/en/function.simplexml-load-string.php
http://php.net/manual/en/simplexml.examples-basic.php

Looking at your XML sample code, it is fairly well formed and has everything you need to pull out your
data in just a few simple lines of code. 100 times faster than all those nested loops. Those nested loops
make bad use of the server’s resources. One simple line of code will take all of your XML tags and load
them into an array. ( $xml = simplexml_load_string($myXMLData); ) Then, you can parse the array with
almost no overhead using other simple commands. So much faster and the correct way to do this.

Sorry if you did not like being told you were doing it the wrong way. Kevin was right and not trying to
annoy you. If you insist to do it “your way”, I will look at your code further, but, really is silly code in my
humble opinion… After you read up on this, let us know which way you want to proceed…

Ernie and Kevin,

I have 18 different xml web hooks that I am going to get pushed. I have to parse all the tags to make them match fields I have to program in to our CMS solutions fields function. I cannot have a field of type “Source” that’s not unique. I need the field to be of type “successful_paymet_notification_transaction_source” and I need the data value for that field to be the value from the XML/array so that I can programmatically use those values in other solutions based on their field names.

@Ernie none of those examples export the Key/Tag values from the XML sheet. Also none of them do it in a nested manner. What method should I use to get TO_FROM_SUBJECT_BODY out of the example? I know how to get the values of those tags.

If there is a better solution for getting a list of nested keys out of an array with nested arrays for use as a single value on the final sub array I would like to know what it is.

However, I do not need help working with the XML portion of this problem. The values have already been placed into an array in the following format.

[php]Array
(
[successful_payment_notification] => Array
(
[account] => Array
(
[account_code] => admin
[username] => admin
[email] => [email protected]
[first_name] => Super_test
[last_name] => Admin_test
[company_name] => Array
(
[@value] =>
[@attributes] => Array
(
[nil] => true
)

                    )

            )

        [transaction] => Array
            (
                [id] => 32cf2700fd814e05ed9cc949279844b3
                [invoice_id] => 32cf2700f0b3f88b7644324214bd2fb3
                [invoice_number_prefix] => 
                [invoice_number] => Array
                    (
                        [@value] => 1098
                        [@attributes] => Array
                            (
                                [type] => integer
                            )

                    )

                [subscription_id] => 32cf2700ab0246a6b14e244bae8cb6b4
                [action] => purchase
                [date] => Array
                    (
                        [@value] => 2015-12-02T01:43:24Z
                        [@attributes] => Array
                            (
                                [type] => datetime
                            )

                    )

                [amount_in_cents] => Array
                    (
                        [@value] => 6490
                        [@attributes] => Array
                            (
                                [type] => integer
                            )

                    )

                [status] => success
                [message] => Successful test transaction
                [reference] => 6914228
                [source] => subscription
                [cvv_result] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [code] => 
                            )

                    )

                [avs_result] => Array
                    (
                        [@value] => Street address and postal code match.
                        [@attributes] => Array
                            (
                                [code] => D
                            )

                    )

                [avs_result_street] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [nil] => true
                            )

                    )

                [avs_result_postal] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [nil] => true
                            )

                    )

                [test] => Array
                    (
                        [@value] => true
                        [@attributes] => Array
                            (
                                [type] => boolean
                            )

                    )

                [voidable] => Array
                    (
                        [@value] => true
                        [@attributes] => Array
                            (
                                [type] => boolean
                            )

                    )

                [refundable] => Array
                    (
                        [@value] => true
                        [@attributes] => Array
                            (
                                [type] => boolean
                            )

                    )

            )

    )

)[/php][/code]

Ernie,

I also understand that this code is not acceptable and silly. I was more trying to prove to myself it could be done and I could get what I wanted. I would never use something like this in production. But I am struggling to find a way to get all the Tags out the way I want them.

Ernie,

$xml = simplexml_load_string($myXMLData); thanks for that clue, the issue there is simplexml strips out the first xml tag of <successful_payment_notification> which makes it so I don’t know what type of web hook I am parsing…

I guess it’s technically the Document Tag.

Well, it doesn’t actually strip anything out. But, it uses it as the top level tag. It is expecting a XML type
to be sent to it first. SO, a very simple work-around would be to just add that tag to your current string.
Something like this would work:

$yourXML = your data pulled out

$newXML = “<?xml version='1.0' encoding='UTF-8'?>” . $yourXML;

This basically will tell the XML parser that you are using standard XML even if you are not…

Leaving for several hours. I will check back in later to see if you solve it or not… Good luck!

One more thought… If you use the simple XML, it gives you an array that holds all of your data with tags.

Therefore,
$xml = simplexml_load_string($myXMLData);
Should give you an array of the things you need. So to look at the “account” values, it should be loosely,
just $account=$xml[“account”]; Then, that result would contain all of the sub items.

So, to pull out, let’s say just the account’s email, it would be something again loosely like
$email = $xml[“account”][“email”];

I did not test that, just wanted to point out the logic of it to you. No nested loops, just pull out the data
that you need and place it where you want it…

That was my first instinct, but after reviewing the XML examples from the vender I figured out I would have something like 16 case statements one for each push type then about 5-6 functions to handle each piece of the xml push and I really wanted it to be dynamic so that I didn’t have to add a ton to the code if they change their notifications.

And we have a solution… or at least the makings of a solution!

[php]function array_flat($array, $prefix = ‘’)
{
$result = array();

foreach ($array as $key => $value)
{
    $new_key = $prefix . (empty($prefix) ? '' : '_') . $key;

    if (is_array($value))
    {
        $result = array_merge($result, array_flat($value, $new_key));
    }
    else
    {
        $result[$new_key] = $value;
    }
}

return $result;

}
[/php]

the output

[php]Array
(
[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_transaction_id] => 32cf2700fd814e05ed9cc949279844b3
[successful_payment_notification_transaction_invoice_id] => 32cf2700f0b3f88b7644324214bd2fb3
[successful_payment_notification_transaction_invoice_number_prefix] =>
[successful_payment_notification_transaction_invoice_number_@value] => 1098
[successful_payment_notification_transaction_invoice_number_@attributes_type] => integer
[successful_payment_notification_transaction_subscription_id] => 32cf2700ab0246a6b14e244bae8cb6b4
[successful_payment_notification_transaction_action] => purchase
[successful_payment_notification_transaction_date_@value] => 2015-12-02T01:43:24Z
[successful_payment_notification_transaction_date_@attributes_type] => datetime
[successful_payment_notification_transaction_amount_in_cents_@value] => 6490
[successful_payment_notification_transaction_amount_in_cents_@attributes_type] => integer
[successful_payment_notification_transaction_status] => success
[successful_payment_notification_transaction_message] => Successful test transaction
[successful_payment_notification_transaction_reference] => 6914228
[successful_payment_notification_transaction_source] => subscription
[successful_payment_notification_transaction_cvv_result_@value] =>
[successful_payment_notification_transaction_cvv_result_@attributes_code] =>
[successful_payment_notification_transaction_avs_result_@value] => Street address and postal code match.
[successful_payment_notification_transaction_avs_result_@attributes_code] => D
[successful_payment_notification_transaction_avs_result_street_@value] =>
[successful_payment_notification_transaction_avs_result_street_@attributes_nil] => true
[successful_payment_notification_transaction_avs_result_postal_@value] =>
[successful_payment_notification_transaction_avs_result_postal_@attributes_nil] => true
[successful_payment_notification_transaction_test_@value] => true
[successful_payment_notification_transaction_test_@attributes_type] => boolean
[successful_payment_notification_transaction_voidable_@value] => true
[successful_payment_notification_transaction_voidable_@attributes_type] => boolean
[successful_payment_notification_transaction_refundable_@value] => true
[successful_payment_notification_transaction_refundable_@attributes_type] => boolean
)[/php]

Short on time… But…

So, if you have a first tag that tells where it comes from, just use that as an index into your own array
to save all of the data from it in your array. Not really sure what your question is.

If you have 16 feeds coming in, you need to label them or index them to send them out. So, index them
on the sender’s first tag. But, normally, you would do it on the transaction ID.

I guess it depends on how you need to send the data back out and in what format. In your first post, you
showed the sample XML for one transaction. That is extremely easy to pull out of the XML in the manner
that I have discussed. If you have 16 of them, just parse each in a small loop.

Note that this array is a simple xml object version. Therefore, you need to pull out the values in a different
way. Something like this: echo $xml->account[0]->email . “
”;
Here is a sample file. Copy it to your sever and run it… Hope it helps…
[php]

XML...
<?PHP

$yourXML = ’<?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>’;

$xml = simplexml_load_string($yourXML);
echo “Account Info:


”;
echo $xml->account[0]->first_name . " " . $xml->account[0]->last_name . “
”;
echo $xml->account[0]->email . “


Transaction:

”;
echo $xml->transaction[0]->invoice_id;
// etc…
echo “

XML array:

”;
print_r($xml);

?>

[/php] If you have 16 tansactions, concatenate them into one string and loop thru them from 0 to 15... Gotta head out... Hope this works for you...

In case anyone cares this is what the final function ended up like so we could get it into our 32 character limit fields. If anyone has some suggestions on how to improve this code I am open to hearing them.

The original XML
[php]<?xml version="1.0" encoding="UTF-8"?>
<new_subscription_notification>

<account_code>test</account_code>
admin
[email protected]
<first_name>Super_test</first_name>
<last_name>Admin_test</last_name>
<company_name nil=“true”></company_name>



<plan_code>membership</plan_code>
Membership

32eced5275cf5f13a1749445ecba8622
active
1
<total_amount_in_cents type=“integer”>28470</total_amount_in_cents>
<subscription_add_ons type=“array”>
<subscription_add_on>
<add_on_code>Family-Member-Add-On</add_on_code>
Add a Family Member
2
<unit_amount_in_cents type=“integer”>5995</unit_amount_in_cents>
</subscription_add_on>
<subscription_add_on>
<add_on_code>Shipping-and-Handling</add_on_code>
Shipping and Handling
3
<unit_amount_in_cents type=“integer”>495</unit_amount_in_cents>
</subscription_add_on>
</subscription_add_ons>
<activated_at type=“datetime”>2015-12-07T20:29:01Z</activated_at>
<canceled_at type=“datetime” nil=“true”></canceled_at>
<expires_at type=“datetime” nil=“true”></expires_at>
<current_period_started_at type=“datetime”>2015-12-07T20:29:01Z</current_period_started_at>
<current_period_ends_at type=“datetime”>2016-12-07T20:29:00Z</current_period_ends_at>
<trial_started_at type=“datetime” nil=“true”></trial_started_at>
<trial_ends_at type=“datetime” nil=“true”></trial_ends_at>
<collection_method>automatic</collection_method>

</new_subscription_notification>
[/php]

The array after converting the XML.

[php]Array
(
[new_subscription_notification] => Array
(
[account] => Array
(
[account_code] => test
[username] => admin
[email] => [email protected]
[first_name] => Super_test
[last_name] => Admin_test
[company_name] => Array
(
[@value] =>
[@attributes] => Array
(
[nil] => true
)

                    )

            )

        [subscription] => Array
            (
                [plan] => Array
                    (
                        [plan_code] => membership
                        [name] => Membership
                    )

                [uuid] => 32eced5275cf5f13a1749445ecba8622
                [state] => active
                [quantity] => Array
                    (
                        [@value] => 1
                        [@attributes] => Array
                            (
                                [type] => integer
                            )

                    )

                [total_amount_in_cents] => Array
                    (
                        [@value] => 28470
                        [@attributes] => Array
                            (
                                [type] => integer
                            )

                    )

                [subscription_add_ons] => Array
                    (
                        [subscription_add_on] => Array
                            (
                                [0] => Array
                                    (
                                        [add_on_code] => Family-Member-Add-On
                                        [name] => Add a Family Member
                                        [quantity] => Array
                                            (
                                                [@value] => 2
                                                [@attributes] => Array
                                                    (
                                                        [type] => integer
                                                    )

                                            )

                                        [unit_amount_in_cents] => Array
                                            (
                                                [@value] => 5995
                                                [@attributes] => Array
                                                    (
                                                        [type] => integer
                                                    )

                                            )

                                    )

                                [1] => Array
                                    (
                                        [add_on_code] => Shipping-and-Handling
                                        [name] => Shipping and Handling
                                        [quantity] => Array
                                            (
                                                [@value] => 3
                                                [@attributes] => Array
                                                    (
                                                        [type] => integer
                                                    )

                                            )

                                        [unit_amount_in_cents] => Array
                                            (
                                                [@value] => 495
                                                [@attributes] => Array
                                                    (
                                                        [type] => integer
                                                    )

                                            )

                                    )

                            )

                        [@attributes] => Array
                            (
                                [type] => array
                            )

                    )

                [activated_at] => Array
                    (
                        [@value] => 2015-12-07T20:29:01Z
                        [@attributes] => Array
                            (
                                [type] => datetime
                            )

                    )

                [canceled_at] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [type] => datetime
                                [nil] => true
                            )

                    )

                [expires_at] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [type] => datetime
                                [nil] => true
                            )

                    )

                [current_period_started_at] => Array
                    (
                        [@value] => 2015-12-07T20:29:01Z
                        [@attributes] => Array
                            (
                                [type] => datetime
                            )

                    )

                [current_period_ends_at] => Array
                    (
                        [@value] => 2016-12-07T20:29:00Z
                        [@attributes] => Array
                            (
                                [type] => datetime
                            )

                    )

                [trial_started_at] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [type] => datetime
                                [nil] => true
                            )

                    )

                [trial_ends_at] => Array
                    (
                        [@value] => 
                        [@attributes] => Array
                            (
                                [type] => datetime
                                [nil] => true
                            )

                    )

                [collection_method] => automatic
            )

    )

)[/php]

The functions used to parse the array.

[php]
function validateDate($date)
{
if (preg_match(’/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/’, $date, $parts) == true) {
$time = gmmktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]);

    $input_time = strtotime($date);
    if ($input_time === false) return false;

    return $input_time == $time;
} else {
    return false;
}

}

function array_flat($array, $prefix = ‘’)
{
$result = array();

foreach ($array as $key => $value)
{
    if ($key[0] == '@') {
        $new_key = $prefix . (empty($prefix) ? '' : '');
    } else {
    	$new_key = $prefix . (empty($prefix) ? '' : '_') . $key;
	}

	$cut_key = substr($new_key, -3);

	if (is_array($value)) {
        $result = array_merge($result, array_flat($value, $new_key));
	} elseif ($cut_key == 'ype' or $cut_key == 'nil') {

$result[$new_key] = $value;

    } else {
		$add = substr($new_key, 0, 2).'_';
		$strlen = strlen( $new_key );
		for( $i = 0; $i <= $strlen; $i++ ) {
		    $char = substr( $new_key, $i, 1 );
		    // $char contains the current character, so do your processing here
		    if ($char == '_') {
			   $add .= substr( $new_key, $i+1, 2);
			   if (substr($add, -1) == '_') {
				   $add = substr($add, 0, -1).'_';
			   } else {
				   $add .= '_';
			   }
		    }
		}
		$add = substr($add, 0, -1);
		if ( strlen($add) >= 23 && ((substr($add, 3, +6)) == (substr($add, 12, +6))) ) {
			$add = substr($add, 0, +3).substr($add, 12);
		}
		if ( (substr($add, 0, +3)) == (substr($add, 3, +3)) ) {
			$add = substr($add, 3);
		}

		$add = 'field_rw_'.$add;

		if (is_numeric($value)) {
			settype($value, 'integer');
		}
		if (validateDate($value)) {
			$value = strtotime($value);
		}
		if ($value == '') {
			$value = NULL;
		}
        $result[$add] = $value;
    }
}
return $result;

}
[/php]

The output of these functions.

[php]Array
(
[field_rw_no] => new_subscription_notification
[field_rw_ac_co] => test
[field_rw_ac_us] => admin
[field_rw_ac_em] => [email protected]
[field_rw_ac_fi_na] => Super_test
[field_rw_ac_la_na] => Admin_test
[field_rw_ac_co_na] =>
[field_rw_su_pl_pl_co] => membership
[field_rw_su_pl_na] => Membership
[field_rw_su_uu] => 32eced5275cf5f13a1749445ecba8622
[field_rw_su_st] => active
[field_rw_su_qu] => 1
[field_rw_su_to_am_in_ce] => 28470
[field_rw_su_ad_on_0_ad_on_co] => Family-Member-Add-On
[field_rw_su_ad_on_0_na] => Add a Family Member
[field_rw_su_ad_on_0_qu] => 2
[field_rw_su_ad_on_0_un_am_in_ce] => 5995
[field_rw_su_ad_on_1_ad_on_co] => Shipping-and-Handling
[field_rw_su_ad_on_1_na] => Shipping and Handling
[field_rw_su_ad_on_1_qu] => 3
[field_rw_su_ad_on_1_un_am_in_ce] => 495
[field_rw_su_ac_at] => 1449520141
[field_rw_su_ca_at] =>
[field_rw_su_ex_at] =>
[field_rw_su_cu_pe_st_at] => 1449520141
[field_rw_su_cu_pe_en_at] => 1481142540
[field_rw_su_tr_st_at] =>
[field_rw_su_tr_en_at] =>
[field_rw_su_co_me] => automatic
)
string: value :new_subscription_notification
string: value :test
string: value :admin
string: value :[email protected]
string: value :Super_test
string: value :Admin_test
NULL: value :
string: value :membership
string: value :Membership
string: value :32eced5275cf5f13a1749445ecba8622
string: value :active
integer: value :1
integer: value :28470
string: value :Family-Member-Add-On
string: value :Add a Family Member
integer: value :2
integer: value :5995
string: value :Shipping-and-Handling
string: value :Shipping and Handling
integer: value :3
integer: value :495
integer: value :1449520141
NULL: value :
NULL: value :
integer: value :1449520141
integer: value :1481142540
NULL: value :
NULL: value :
string: value :automatic[/php]

It occurs to me now that I could use the attribute fields in the XML/Array to do the checking for integers and date times that I do after it is all in a flat Array but I don’t know that it would improve the function or code in anyway.

Well, congrats on your solution. Nice to solve a complex programming puzzle! CYA in the next one…

Sponsor our Newsletter | Privacy Policy | Terms of Service