Jump to content


Photo

Array to XML while changing the structure


  • Please log in to reply
3 replies to this topic

#1 BrandonK

BrandonK
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 19 October 2006 - 07:42 PM

I have an array of products in a format like:
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
        )

I would like to convert this to an array and then an XML file that will end up in a format like this:
<?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>

The Array structure for that would probably look like:
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
                                        )
                                )
                        )
                )
        )
)

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?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 19 October 2006 - 11:26 PM

This will give you a start
<?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>';
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Skatecrazy1

Skatecrazy1
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts
  • LocationSouthern California

Posted 20 October 2006 - 01:55 AM

probably just echo some root tags, then use a foreach to echo the tags that go around the info, plus the info.
fuck it or fight it

#4 BrandonK

BrandonK
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 20 October 2006 - 04:02 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users