Page 1 of 1

iteration through an array

Posted: Mon May 14, 2007 8:53 pm
by yanisdon
Hi,
I am new to this, so I hope you can give me a hand.

My data source is a generic XML file. With the help of PHP's SimpleXML I am actually able to parse the XML file into an object/array.
So far so good.

Now I like to loop through that array and write the data appropriately into a mySQL-DB width correlative field names, whereas $rss->items contains the data.

With the following foreach I only get something like this:

Code: Select all

foreach ($rss->items as $a => $b) {
				
                   echo $a,'="',$b,"\"\n";
		
	   }

0="Array"
1="Array"
2="Array"



I have the following array:

Code: Select all

[parser] => Resource id #10
    [current_item] => Array
        (
        )

    [items] => Array
        (
            [0] => Array
                (
                    [title] => test date indexing
                    [link] => http://www.google.com
                    [dsm] => Array
                        (
                            [source] => testdocs
                            [relevance] => 1.0
                            [metadata_score] => 1.0
                            [metadata_collection] => Test Institute Documents
                            [metadata_collectionid] => 3
                            [metadata_collectionurl] => http://test/testspace/handle/10096/4
                            [metadata_content] => 1244095381
                            [metadata_dc.date.accessioned] => 2007-05-07T04:54:22Z
                            [metadata_dc.date.available] => 2007-05-07T04:54:22Z
                            [metadata_dc.date.copyright] => 2007-01-01T00:00:00Z
                            [metadata_dc.date.created] => 2006-01-01T00:00:00Z
                            [metadata_dc.date.issued] => 2007-05-07T04:54:22Z
                            [metadata_dc.date.lastmodified] => 2007-05-07T14:24:22Z
                            [metadata_dc.description.provenance] => Submitted by Peter Tester (test@test.com) on 2007-05-07T04:54:22ZNo. of bitstreams: 0
                            [metadata_dc.identifier.uri] => http://www.google.com
                            [metadata_dc.language] => en
                            [metadata_dc.title] => test date indexing
                            [metadata_filename] => www.google.com
                            [metadata_id] => 1244095381
                            [metadata_mimetype] => text/html
                            [metadata_size] => 0
                            [metadata_url] => http://www.google.com
                        )

                )

            [1] => Array
                (
                    [title] => test
                    [link] => http://test/testspace/handle/10096/676
                    [dsm] => Array
                        (
                            [source] => testdocs
                            [relevance] => 0.85
                            [metadata_score] => 1.0
                            [metadata_collection] => Carrick Exchange
                            [metadata_collectionid] => 4
                            [metadata_collectionurl] => http://test/testspace/handle/10096/663
                            [metadata_content] => 441
                            [metadata_dc.creator] => smith
                            [metadata_dc.date.accessioned] => 2007-05-07T00:00:00Z
                            [metadata_dc.date.available] => 2007-05-07T00:00:00Z
                            [metadata_dc.date.issued] => 2007-05-07T00:00:00Z
                            [metadata_dc.date.lastmodified] => 2007-05-07T09:37:24Z
                            [metadata_dc.description.provenance] => Submitted by Peter Tester (test@test.com) on 2007-05-07No. of bitstreams: 1200204-drft-IRS.pdf: 316 bytes, checksum: 1fad7f4d2781dc59da1b732d9755cc6e (MD5)
                            [metadata_dc.identifier.uri] => http://test/testspace/handle/10096/678
                            [metadata_dc.language.iso] => en
                            [metadata_dc.title] => test
                            [metadata_filename] => 200204-drft-IRS.pdf
                            [metadata_id] => 441
                            [metadata_mimetype] => application/pdf
                            [metadata_size] => 316
                            [metadata_url] => http://test/testspace/handle/10096/676
                        )

                )

            [2] => Array
                (
                    [title] => Test date indexing 1
                    [link] => http://test/testspace/handle/10096/681
                    [dsm] => Array
                        (
                            [source] => testdocs
                            [relevance] => 0.70000005
                            [metadata_score] => 1.0
                            [metadata_collection] => Test Institute Documents
                            [metadata_collectionid] => 3
                            [metadata_collectionurl] => http://test/testspace/handle/10096/4
                            [metadata_content] => 444
                            [metadata_dc.date.accessioned] => 2007-05-07T05:04:57Z
                            [metadata_dc.date.available] => 2007-05-07T05:04:57Z
                            [metadata_dc.date.copyright] => 2007-02-01T00:00:00Z
                            [metadata_dc.date.created] => 2007-01-01T00:00:00Z
                            [metadata_dc.date.issued] => 2007-05-07T05:04:57Z
                            [metadata_dc.date.lastmodified] => 2007-05-07T14:37:32Z
                            [metadata_dc.description.provenance] => Submitted by Peter Tester (test@test.com) on 2007-05-07T05:04:57Z
No. of bitstreams: 1
46hippo.jpg: 58854 bytes, checksum: bff9237d26b1793ebe9bbee7c4715826 (MD5)
                            [metadata_dc.identifier.uri] => http://test/testspace/handle/10096/683
                            [metadata_dc.language] => en
                            [metadata_dc.title] => Test date indexing 1
                            [metadata_filename] => 46hippo.jpg
                            [metadata_id] => 444
                            [metadata_mimetype] => image/jpeg
                            [metadata_size] => 58854
                            [metadata_url] => http://test/testspace/handle/10096/681
                        )

                )

        )

Re: iteration through an array

Posted: Mon May 14, 2007 9:37 pm
by volka
yanisdon wrote:and write the data appropriately into a mySQL-DB width correlative field names
Exactly how does your table look like? Or are there tables?

Posted: Mon May 14, 2007 10:02 pm
by yanisdon
Hi,
Thanks for the prompt response.

Well, the unique identifier would be

Code: Select all

[items] [$i] [dsm] [metadata_dc.identifier.uri] => http://test/testspace/handle/10096/678
It is basically always a string [with a file handle].

You are right, I've got to map the data to the appropriate fieldname in the DB, let's assume they are named after the $key => part of the array (e.g. [metadata_dc.creator] would map to [metadata_dc.creator] in mySQL).

So essentially I'd like to find an apporach for the loop.

Cheers

Posted: Tue May 15, 2007 4:30 am
by volka
try

Code: Select all

foreach($rss->items as $item) {
	echo 'title: ', $item->title, "<br />\n";
	echo 'link: ', $item->link, "<br />\n";
	foreach($item->dsm[0] as $key=>$value) {
		echo $key, ': ', $value, "\n";
	}
}