By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,053 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,053 IT Pros & Developers. It's quick & easy.

convert XML feed array to assocative

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.