Jump to content

[SOLVED] extracting array elements


smilesmita

Recommended Posts

hi there,

this is a kind of array i get after i have parsed my xml.

now i wanted to extract data from this array:

name and cdata elements needs to be extracted.any ideas?

[2] => Array
        (
            [name] => DELIVERY
            [attrs] => Array
                (
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => PACKAGEREFERENCENUMBER
                            [attrs] => Array
                                (
                                )

                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => CODE
                                            [attrs] => Array
                                                (
                                                )

                                            [cdata] => IK
                                        )

                                    [1] => Array
                                        (
                                            [name] => VALUE
                                            [attrs] => Array
                                                (
                                                )

                                            [cdata] => 66759
                                        )

                                )

                        )

Link to comment
https://forums.phpfreaks.com/topic/63604-solved-extracting-array-elements/
Share on other sites

<?php
$data = array (
    '2' => Array
        (
            'name' => 'DELIVERY',
            'attrs' => Array
                (
                ),

            'children' => Array
                (
                    '0' => Array
                        (
                            'name' => 'PACKAGEREFERENCENUMBER',
                            'attrs' => Array
                                (
                                ),

                            'children' => Array
                                (
                                    '0' => Array
                                        (
                                            'name' => 'CODE',
                                            'attrs' => Array
                                                (
                                                ),

                                            'cdata' => IK
                                        ),

                                    '1' => Array
                                        (
                                            'name' => 'VALUE',
                                            'attrs' => Array
                                                (
                                                ),

                                            'cdata' => 66759
                                        )

                                )

                        )
                   )
            )
  );
  
foreach ($data[2]['children'][0]['children'] as $childArray)
{
    echo 'Name : ', $childArray['name'], '<br>';
    echo 'CData : ', $childArray['cdata'], '<br><br>'; 
} 
?>

hi thanks for he solution.i have one question on that though...

how do i convert my array to the modifications you have done to my array.

like u added "data=

and 'children','0'..?

 

that is the array  when i convert xml into array?

any idea abt how i can get this array matched with the array you have shown in your solution?>

 

Thanks again !

 

I added $data because I had to call it something. I have no way of knowing what you called it, and as it was an extract, starting at element 2, I have no way of knowing the full structure of your array.

 

The "children" and "0" etc are the keys in your array, not added by me.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.