Jump to content

Array to XML while changing the structure


BrandonK

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?
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.