tags:

views:

50

answers:

1

I'm trying to automate my Amazon order processing. Currently I'm using Amazon Merchant Transport to get the XML file for an order, but eventually, I'll want to get the xml files with a web-service directly to my seller central account.

Partial array with Amazon order info -

Array
(
...
        [37] => Array
        (
            [tag] => NAME
            [type] => complete
            [level] => 6
            [value] => John Smith
        )
...
    [39] => Array
        (
            [tag] => ADDRESSFIELDONE
            [type] => complete
            [level] => 6
            [value] => 100 N MAIN St.
        )
...
    [41] => Array
        (
            [tag] => CITY
            [type] => complete
            [level] => 6
            [value] => PLANO
        )
...
    [43] => Array
        (
            [tag] => STATEORREGION
            [type] => complete
            [level] => 6
            [value] => TX
        )
...
    [45] => Array
        (
            [tag] => POSTALCODE
            [type] => complete
            [level] => 6
            [value] => 75075-1948
        )

)

I am able to retrieve my orders as XML documents and can parse them with the following code.

$contents = file_get_contents("amzn-order-sample.xml");
$p = xml_parser_create();
xml_parse_into_struct($p, $contents, $vals, $index);
xml_parser_free($p);

Now I'm trying to pull out the order related information - first the one time information (shipper name, address)

then I want to be able to get the 'items ordered/quantity' information, which could be 1->many items for this customer.

Sample code that I have working -- to retrieve the value (which I will put into another array for further processing) (pretty ugly)

$arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($vals));

// shipper name
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'NAME') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper Name\n";
    print_r($outputArray);
    }
}

// shipper phone
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'BUYERPHONENUMBER') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper Phone\n";
    print_r($outputArray);
    }
}

// shipper addresslineone
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'ADDRESSFIELDONE') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper Address\n";
    print_r($outputArray);
    }
}

// shipper city
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'CITY') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper City\n";
    print_r($outputArray);
    }
}

// shipper state
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'STATEORREGION') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper State\n";
    print_r($outputArray);
    }
}

// shipper zip
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'POSTALCODE') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper Zip\n";
    print_r($outputArray);
    }
}
//  items
 foreach ($arrIt as $sub) {
    $subArray = $arrIt->getSubIterator();
    if ($subArray['tag'] === 'POSTALCODE') {
        $outputArray[] = iterator_to_array($subArray);
    echo "Shipper Zip\n";
    print_r($outputArray);
    }
}

I was going to parse the 1-many items per order in a loop with the above code, but there must be a better, cleaner way.. -- maybe someone has code snippets they can offer.

Thanks in advance for any ideas..

a Newbie to PHP

There must be many better ways to do this type of code in XML and PHP (this will be processed on a Linux server) -- any suggestions or other ideas are greatly welcome

A: 

You only need one foreach loop, as each one is doing the same thing, but looking for different info.

Something like:

 foreach ($order_info_array as $sub) 
 {
    switch ($sub['tag'])
    {
        case 'NAME':
            echo "Shipper name\n";
            break;
        case 'BUYERPHONENUMBER':
            echo "Shipper Phone\n";
            break; 
    }

    echo $sub['value'];
}

Take a look at the manual entry for the switch statement: http://php.net/manual/en/control-structures.switch.php

Note, in case it isn't obvious. $order_info_array is the full version of that massive array you've posted.

George Marian
Thank you very much -- this was the information I was trying to understand and find in my array example searching
@newbiephper You're quite welcome. You should accept the answer, by clicking the check mark next to it. I also suggest reading the faq to get an idea how stackoverflow functions.
George Marian