Jump to content

Recommended Posts

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:

             foreach ($rss->items as $a => $b) {

                   echo $a,'="',$b,"\"\n";

   }

0="Array"

1="Array"

2="Array"

 

 

I have the following array:

 

[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
                        )

                )

        )

Link to comment
https://forums.phpfreaks.com/topic/51418-iteration-through-an-array/
Share on other sites

No, problem. But, first you have to specify the structure of your database tables and which fields in the array map to which fields in the database.

 

EDIT: Also, one piece of inportant information would be whether or not you would be inserting the same records multiple times. I would assume so since these are RSS feeds. So, you would have to decide which piece of data is the unique identifier for each record - the URL?

Hi,

Thanks for the prompt response.

 

Well, the unique identifier would be  [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, but for the moment 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

EDIT:

 

Something I like to add:

 

if I do this I'll actually get that:

foreach ($rss->items as $a => $b) {
                print_r ($b);
        }        

 

 

     
        Array
(
   [title] => test date indexing
                    [link] => (URL address blocked: See forum rules)
                    [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] => (URL address blocked: See forum rules)
                            [metadata_dc.language] => en
                            [metadata_dc.title] => test date indexing
                            [metadata_filename] => (URL address blocked: See forum rules)
                            [metadata_id] => 1244095381
                            [metadata_mimetype] => text/html
                            [metadata_size] => 0
                            [metadata_url] => (URL address blocked: See forum rules)
                        )

)
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
                        )

)
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
                        )	

)

//print_r ($b['dsm']); would therefore contain:

 

                /* 
            [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] => (URL address blocked: See forum rules)
            [metadata_dc.language] => en
            [metadata_dc.title] => test date indexing
            [metadata_filename] => (URL address blocked: See forum rules)
            [metadata_id] => 1244095381
            [metadata_mimetype] => text/html
            [metadata_size] => 0
            [metadata_url] => (URL address blocked: See forum rules)
               */

 

Now I've got to write my INSERT stuff within that loop, right?

 

Cheers

Try this:

 

<?php
$valuesList = array();

foreach ($rss->items as $rss_array) {

    $thisRecord = array();

    //Put these in the order that you want them inserted into the database
    $thisRecord[] = $rss_array[dsm][metadata_dc.identifier.uri];
    $thisRecord[] = $rss_array[title];
    $thisRecord[] = $rss_array[link];
    $thisRecord[] = $rss_array[dsm][source];
    $thisRecord[] = $rss_array[dsm][[relevance];
    $thisRecord[] = $rss_array[dsm][metadata_score];
    $thisRecord[] = $rss_array[dsm][metadata_collection];
    $thisRecord[] = $rss_array[dsm][metadata_collectionid];
    $thisRecord[] = $rss_array[dsm][metadata_collectionurl];
    $thisRecord[] = $rss_array[dsm][metadata_content];
    $thisRecord[] = $rss_array[dsm][metadata_dc.date.accessioned];
    $thisRecord[] = $rss_array[dsm][metadata_dc.date.available];
    $thisRecord[] = $rss_array[dsm][metadata_dc.date.copyright];
    $thisRecord[] = $rss_array[dsm][metadata_dc.date.created];
    $thisRecord[] = $rss_array[dsm][metadata_dc.date.issued];
    $thisRecord[] = $rss_array[dsm][metadata_dc.date.lastmodified];
    $thisRecord[] = $rss_array[dsm][metadata_dc.description.provenance];
    $thisRecord[] = $rss_array[dsm][metadata_dc.language];
    $thisRecord[] = $rss_array[dsm][metadata_dc.title];
    $thisRecord[] = $rss_array[dsm][metadata_filename];
    $thisRecord[] = $rss_array[dsm][metadata_id];
    $thisRecord[] = $rss_array[dsm][metadata_mimetype];
    $thisRecord[] = $rss_array[dsm][metadata_size];
    $thisRecord[] = $rss_array[dsm][metadata_url];

    $valuesList[] = "('" . implode("','", $thisRecord[]) . "')";
    unset($thisRecord[]);

}

$query = 'INSERT INTO table (identifier, title, link, source, relevance, score, collection,
                             collectionid, collectionurl, content, accessioned, available,
                             copyright, created, issued, lastmodified, provenance, language,
                             meta_title, filename, meta_id, mimetype, size, url)
          VALUES ' . implode(",", $valuesList);

echo $query;

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.