473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1561
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*******@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.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*******@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.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
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...
6
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...
5
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
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? ...
1
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...
1
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...
1
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...
1
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...
40
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.