Dynamic array creation and tracking
am 06.10.2009 18:18:38 von John NichelHowdy guys and girls. Been a long time since I've been in these parts.
My php is rusty these days, so please bare with me. I'm trying to
create a dynamic, multidimensional array, and keep track of which level
I'm on, opening and closing levels as needed. Lets say that I have some
data like the below:
Array
(
[0] =3D> Array
(
[tag] =3D> OrderFeed
[type] =3D> open
[level] =3D> 1
[attributes] =3D> Array
(
[version] =3D> 1.0
)
[value] =3D> -
)
[1] =3D> Array
(
[tag] =3D> Order
[type] =3D> open
[level] =3D> 2
[value] =3D> -
)
[2] =3D> Array
(
[tag] =3D> OrderId
[type] =3D> open
[level] =3D> 3
)
[3] =3D> Array
(
[tag] =3D> E4XOrderId
[type] =3D> complete
[level] =3D> 4
[value] =3D> E4X000000000001
)
[4] =3D> Array
(
[tag] =3D> MerchantOrderId
[type] =3D> complete
[level] =3D> 4
[value] =3D> Mrc0000001
)
[5] =3D> Array
(
[tag] =3D> MerchantOrderRef
[type] =3D> complete
[level] =3D> 4
[value] =3D> ABCDEFGHI000001
)
[6] =3D> Array
(
[tag] =3D> OrderId
[type] =3D> close
[level] =3D> 3
)
[7] =3D> Array
(
[tag] =3D> Order
[value] =3D> -
[type] =3D> cdata
[level] =3D> 2
)
[8] =3D> Array
(
[tag] =3D> OrderDate
[type] =3D> open
[level] =3D> 3
)
[9] =3D> Array
(
[tag] =3D> CreateDate
[type] =3D> complete
[level] =3D> 4
[value] =3D> 12-03-2007
)
[10] =3D> Array
(
[tag] =3D> ExpiryDate
[type] =3D> complete
[level] =3D> 4
[value] =3D> 12-15-2007
)
)
I create an empty array before I start looping through this data:
$newArray =3D array();
Now, when looping through the data, every time I encounter a 'tag' that
is of the open 'type' I need to create a new array within the base array
with the value of 'tag' as the index:
$newArray['OrderFeed']
$newArray['OrderFeed']['Order']
$newArray['OrderFeed']['Order']['OrderID']
So on and so forth. When I get to a 'tag' that is of the 'type'
complete, I need to make that a name value pair in the current level of
the array:
Array
(
[OrderFeed] =3D> Array
(
[Order] =3D Array
(
[OrderID] =3D Array
(
[CreateDate] =3D> 12-03-2007
)
)
)
)
And when I get to 'tag' of the close 'type', I need to move up one level
in the array. I have tried using another array just to keep track of
what level of the array I'm currently on, but I can seem to figure out
how to form the master array out of it. In the above example, I would
have an array with the 'OrderFeed', 'Order' and 'OrderID' as the three
elements in it. If I encounter and 'open' tag, I add that value onto
the end of the 'tracking' array, and when I encounter a 'close' tag, I
just pop the last element off of the tracking array. So basically, I'm
trying to take the values in my 'tracking' array....
Array
(
OrderFeed,
Order,
OrderID
)
And somehow, use them to keep track of where I am in the dynamic
$newArray. I hope this makes sense....I know what I want to do in my
head, but I'm not sure I'm getting it out well here. Any suggestions?
Questions?
--
John C. Nichel IV
System Administrator
KegWorks
http://www.kegworks.com
716.362.9212 x16
john@kegworks.com=20
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php