Need more speed in PHP foreach loop
<p>I'm doing some integrations towards MS based web applications which forces me to fetch the data to my php application via SOAP which is fine.</p>

<p>I got the structure of a file system in an xml which I convert to an object. All documents have an ID and it's path. To be able to place the documents in a tree view I've built some methods to calculate the documents whereabouts through the files and folder structure. This works fine until I started to try with large file lists.</p>

<p>What I need is a faster method (or way to do things) than a foreach loop.</p>

<p>The method below is the troublemaker.</p>

<pre><code>/**
 * Find parent id based on path
 * @param array $documents
 * @param string $parentPath
 * @return int 
 */
private function getParentId($documents, $parentPath) {
    $parentId = 0;
    foreach ($documents as $document) {
        if ($parentPath == $document-&gt;ServerUrl) {
            $parentId = $document-&gt;ID;
            break;
        }
    }
    return $parentId;
}
// With 20 documents nested in different folders this method renders in 0.00033712387084961
// With 9000 documents nested in different folders it takes 60 seconds
</code></pre>

<p>The array sent to the object looks like this</p>

<pre><code>Array
(
    [0] =&gt; testprojectDocumentLibraryObject Object
        (
            [ParentID] =&gt; 0
            [Level] =&gt; 1
            [ParentPath] =&gt; /Shared Documents
            [ID] =&gt; 163
            [GUID] =&gt; 505d70ea-51d7-4ef0-bf79-8e912553249e
            [DocIcon] =&gt; 
            [FileType] =&gt; 
            [Title] =&gt; Folder1
            [BaseName] =&gt; Folder1
            [LinkFilename] =&gt; Folder1
            [ContentType] =&gt; Folder
            [FileSizeDisplay] =&gt; 
            [_UIVersionString] =&gt; 1.0
            [ServerUrl] =&gt; /Shared Documents/Folder1
            [EncodedAbsUrl] =&gt; http://dev1.example.com/Shared%20Documents/Folder1
            [Created] =&gt; 2011-10-08 20:57:47
            [Modified] =&gt; 2011-10-08 20:57:47
            [ModifiedBy] =&gt; 
            [CreatedBy] =&gt; 
            [_ModerationStatus] =&gt; 0
            [WorkflowVersion] =&gt; 1
        )
...
</code></pre>

<p>A bit bigger example of the data array is available here
<a href="http://www.trikks.com/files/testprojectDocumentLibraryObject.txt" rel="nofollow">http://www.trikks.com/files/testprojectDocumentLibraryObject.txt</a></p>

<p>Thanks for any help!</p>

<p>=== UPDATE ===</p>

<p>To illustrate the time different stuff takes I've added this part.</p>

<ol>
<li>Packet downloaded in 8.5031080245972 seconds</li>
<li>Packet decoded in 1.2838368415833 seconds</li>
<li>Packet unpacked in 0.051079988479614 seconds</li>
<li>List data organized in 3.8216209411621 seconds</li>
<li>Standard properties filled in 0.46236896514893 seconds</li>
<li><strong>Custom properties filled in 40.856066942215 seconds</strong></li>
<li>TOTAL: This page was created in 55.231353998184 seconds!</li>
</ol>

<p>Now, this is a custom property action that im describing, the other stuff is already somewhat optimized. The data sent from the WCF service is compressed and encoded ratio 10:1 (like 10mb uncompressed : 1mb compressed). </p>

<p>The current priority is to optimize the custom properties part, where the getParentId method takes 99% of the execution time!</p>
