Jump to content

Archived

This topic is now archived and is closed to further replies.

BrandonK

Array to XML while changing the structure

Recommended Posts

I have an array of products in a format like:
[code]Array
(
    [0] => Array
        (
            [Vendor] => TX
            [ProdCode] => 9320
            [Attribute] => BLACK
            [Size] => S
            [Quantity] => 1
        )
    [1] => Array
        (
            [Vendor] => TX
            [ProdCode] => 9320
            [Attribute] => BLACK
            [Size] => M
            [Quantity] => 5
        )
    [2] => Array
        (
            [Vendor] => TX
            [ProdCode] => 9320
            [Attribute] => WHITE
            [Size] => S
            [Quantity] => 9
        )[/code]

I would like to convert this to an array and then an XML file that will end up in a format like this:
[code]<?xml version="1.0" encoding="utf-8"?>
<inventory>
<vendor>
<Vendor>TX</Vendor>
<item>
<ProdCode>9320</ProdCode>
<attr>
<Attribute>BLACK</Attribute>
<size>
<Size>S</Size>
<Quantity>1</Quantity>
</size>
<size>
<Size>M</Size>
<Quantity>5</Quantity>
</size>
</attr>
<attr>
<Attribute>WHITE</Attribute>
<size>
<Size>S</Size>
<Quantity>9</Quantity>
</size>
</attr>
</item>
</vendor>
</inventory>[/code]

The Array structure for that would probably look like:
[code]Array
(
    [vendor] => Array
        (
            [Vendor] => TX
            [item] => Array
                (
                    [ProdCode] => 9320
                    [attr] => Array
                        (
                            [0] => Array
                                (
                                    [Attribute] => BLACK
                                    [size] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [Size] => S
                                                    [Quantity] => 1
                                                )
                                            [1] => Array
                                                (
                                                    [Size] => M
                                                    [Quantity] => 5
                                                )
                                        )
                                )
                            [1] => Array
                                (
                                    [Attribute] => WHITE
                                    [size] => Array
                                        (
                                            [Size] => S
                                            [Quantity] => 9
                                        )
                                )
                        )
                )
        )
)[/code]

I'm open to other ideas, but the main goals are to be able to:
Look up the Quantity quickly if you have the Vendor, ProdCode, Attribute and Size.
Look up all of the Attributes if you have the Vendor and ProdCode.
See if an Attribute is in a Vendor and ProdCode.

Can anyone help me out?

Share this post


Link to post
Share on other sites
This will give you a start
[code]
<?php
$data = array (

    array
        (
            'Vendor' => 'TX',
            'ProdCode' => '9320',
            'Attribute' => 'BLACK',
            'Size' => 'S',
            'Quantity' => '1'
        ),
    array
        (
            'Vendor' => 'TX',
            'ProdCode' => '9320',
            'Attribute' => 'BLACK',
            'Size' => 'M',
            'Quantity' => '5'
        ),
    array
        (
            'Vendor' => 'TX',
            'ProdCode' => '9320',
            'Attribute' => 'WHITE',
            'Size' => 'S',
            'Quantity' => '9'
        )
);

$array = array();
foreach ($data as $item) {
    $array[$item['Vendor']][$item['ProdCode']][$item['Attribute']][] = array (
        'size' => $item['Size'],
        'qty'  => $item['Quantity']
    );
}
echo '<pre>', print_r($array, true), '</pre>';
?>
[/code]

Share this post


Link to post
Share on other sites
probably just echo some root tags, then use a foreach to echo the tags that go around the info, plus the info.

Share this post


Link to post
Share on other sites
I think it's funny how you can work so hard on a project, and completely miss the obvious.  I'm over here trying out all of these PHP classes trying to find one that properly translates an Array to XML in the format I wanted.

Thanks to both you.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.