473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert XML feed array to assocative

I receive the following array via an XML feed:

Array
(
[0] =Array
(
[sub_family_id] =290**
[image_types] =1 2**
[short_name] =Vibratory Soil**
[long_name] =Vibratory Soil Compactors**
[image_id] =C018741**
)

[1] =Array
(
[sub_family_id] =287**
[image_types] =1 2**
[short_name] =Soil**
[long_name] =Soil Compactors**
[image_id] =C018741**
)

[2] =Array
(
[sub_family_id] =286**
[image_types] =1 2**
[short_name] =Landfill**
[long_name] =Landfill Compactors**
[image_id] =C018741**
)

[3] =Array
(
[sub_family_id] =288**
[image_types] =1 2**
[short_name] =Vibratory Asphalt**
[long_name] =Vibratory Asphalt Compactors**
[image_id] =C018741**
)

[4] =Array
(
[sub_family_id] =289**
[image_types] =1 2**
[short_name] =Pneumatic**
[long_name] =Pneumatic Compactors**
[image_id] =C018741**
)

)

What's the best way to make this information easily accessible? My idea is
to convert it to an associative array (trimming the double asterisks in the
process) and use the sub_family_id as the key. I could then simply access
the short_name value "Pneumatic" for sub_family_id "289" like

$array['289']['short_name']

Is this a reasonable approach? Is there a better method?
Jan 27 '07 #1
4 1575
You could certain index the list by sub_family_id:

foreach ($arr as $el) {
$families[substr($el['sub_family_id'], 0, -2)] = $el;
}

On Jan 26, 7:56 pm, "Bosconian" <nob...@nowhere .comwrote:
I receive the following array via an XML feed:

Array
(
[0] =Array
(
[sub_family_id] =290**
[image_types] =1 2**
[short_name] =Vibratory Soil**
[long_name] =Vibratory Soil Compactors**
[image_id] =C018741**
)

[1] =Array
(
[sub_family_id] =287**
[image_types] =1 2**
[short_name] =Soil**
[long_name] =Soil Compactors**
[image_id] =C018741**
)

[2] =Array
(
[sub_family_id] =286**
[image_types] =1 2**
[short_name] =Landfill**
[long_name] =Landfill Compactors**
[image_id] =C018741**
)

[3] =Array
(
[sub_family_id] =288**
[image_types] =1 2**
[short_name] =Vibratory Asphalt**
[long_name] =Vibratory Asphalt Compactors**
[image_id] =C018741**
)

[4] =Array
(
[sub_family_id] =289**
[image_types] =1 2**
[short_name] =Pneumatic**
[long_name] =Pneumatic Compactors**
[image_id] =C018741**
)

)

What's the best way to make this information easily accessible? My idea is
to convert it to an associative array (trimming the double asterisks in the
process) and use the sub_family_id as the key. I could then simply access
the short_name value "Pneumatic" for sub_family_id "289" like

$array['289']['short_name']

Is this a reasonable approach? Is there a better method?
Jan 27 '07 #2
"petersprc" <pe*******@gmai l.comwrote in message
news:11******** **************@ j27g2000cwj.goo glegroups.com.. .
You could certain index the list by sub_family_id:

foreach ($arr as $el) {
$families[substr($el['sub_family_id'], 0, -2)] = $el;
}
Yeah, that's pretty much the idea. Thanks for your reply.
Jan 27 '07 #3
"petersprc" <pe*******@gmai l.comwrote in message
news:11******** **************@ j27g2000cwj.goo glegroups.com.. .
You could certain index the list by sub_family_id:

foreach ($arr as $el) {
$families[substr($el['sub_family_id'], 0, -2)] = $el;
}
I ended up doing this:

foreach ($xml as $array) {
foreach ($array as $element =$value) {
$elements[rtrim($array[$index], '* ')][$element] = rtrim($value, '* ');
}
}

which outputs

Array
(
[290] =Array
(
[sub_family_id] =290
[image_types] =1 2
[short_name] =Vibratory Soil
[long_name] =Vibratory Soil Compactors
[image_id] =C018741
)

[287] =Array
(
[sub_family_id] =287
[image_types] =1 2
[short_name] =Soil
[long_name] =Soil Compactors
[image_id] =C018741
)

[286] =Array
(
[sub_family_id] =286
[image_types] =1 2
[short_name] =Landfill
[long_name] =Landfill Compactors
[image_id] =C018741
)

[288] =Array
(
[sub_family_id] =288
[image_types] =1 2
[short_name] =Vibratory Asphalt
[long_name] =Vibratory Asphalt Compactors
[image_id] =C018741
)

[289] =Array
(
[sub_family_id] =289
[image_types] =1 2
[short_name] =Pneumatic
[long_name] =Pneumatic Compactors
[image_id] =C018741
)

)
Jan 27 '07 #4
I ended up doing this:
>
foreach ($xml as $array) {
foreach ($array as $element =$value) {
$elements[rtrim($array[$index], '* ')][$element] = rtrim($value, '* ');
}
}
$index equals 'sub_family_id' in the above example.
Jan 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3058
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
6
2323
by: Martman | last post by:
I am new to PHP but testing the ability to read an RSS xml feed. I have successfully retrieved, read, and displayed an RSS feed from pcworld. However, when you click on one of the links it adds http://localhost to the link located in the xml file. If I view source in the browser everything appears okay. Does anyone know of a configuration in the apache or php config files that automatically adds http://localhost to hyperlinks? If not,...
5
3561
by: Mayhem | last post by:
I would like to integrate an RSS newsfeed into my website but can't seem to figure out how. Has anyone got any pointers for me? E.
10
5544
by: lawrence | last post by:
Validator chokes on my pages now because I started sending an character encoding header of UTF-8 but the page is full of non UTF-8 characters. Anyway quick way to convert them? http://validator.w3.org/check?uri=http%3A%2F%2Fwww.krubner.com%2F
1
3352
by: Chris Mosser | last post by:
I'm looking to add a page to my site where I can get stock quotes for the wired, but more importantly, the wireless web(ie my cell phone). I know how write the needed php and WAP application, I just don't know where to get the stock market feed, at least not without parsing a finance.yahoo.com page. Or something else like it that could change at any time. I'm looking for just the raw data feed. Anyone ever had to do this and can give me...
1
2369
by: elixxir | last post by:
Hiho, I'm trying to deserialize the following XML feed from the weather channel but I'm having an issue with the nested arrays. I got as far as getting the 'day' into arrays but I can't get the 'part' nested array working. This is my first hack at XML feeds and such, so any pointers as to how to best do this or references/tutorials would be good. (I'm doing all this in .NET 2.0 btw)
1
12054
by: rawCoder | last post by:
Hi, Ok this shud b simple but i cant seem to find it. I want to convert a System.UInt16 data to 2 Byte array and System.UInt32 data to 4 Byte array ( BigEndian) something like Public Sub GetBytes(ByVal n as System.UInt16, ByRef b As Byte()) 'Converts the System.UInt16 to a 2 Byte array
1
3486
by: gaikokujinkyofusho | last post by:
Hi, I have been enjoying being able to subscribe to RSS (http://kinja.com/user/thedigestibleaggie) for awhile and have come up with a fairly nice list of feeds but I have run into an annoying (though not critical) problem, duplicate stories. Apparently there is overlap with some of the sites I subscribe to so I get duplicate stories. Does anyone know of some sort of filter (software or online service) that can remove duplicate stories? Any...
40
1803
by: Tameem | last post by:
hi my name is tameem. i am a new c programmer. in a c program i want to give input ""tameem"" and the output will be ""xdphhp"" in a short: the character ""a"" will be replaced by ""d"" i can convert only a character.so how can i convert a word or a sentence my character convert program is given below: #include<stdio.h>
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7603
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.