BrandonK Posted October 19, 2006 Share Posted October 19, 2006 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2006 Share Posted October 19, 2006 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] Quote Link to comment Share on other sites More sharing options...
Skatecrazy1 Posted October 20, 2006 Share Posted October 20, 2006 probably just echo some root tags, then use a foreach to echo the tags that go around the info, plus the info. Quote Link to comment Share on other sites More sharing options...
BrandonK Posted October 20, 2006 Author Share Posted October 20, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.