Re: parsing objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Marcus,

some facts:

1. you CAN return 'proper' values from a recursive function call.
2. you DONT need a recursive function call to retrieve the properties
of these objects.
3. you DONT need a recursive function to build a nested data structure
(using the parent<->child ID relationship)

with regard to number 3 try having a look at the following code
snippet (I leave it as an exercise to you to figure out how to
translate it to your own situation (my example happens to
use arrays because that's how the data was retrieved from a DB,
you'll want to stick with the collection of objects you get bgack from
the SOAP call - the principle is the same):

the trick is the use of the $parents 'stack' (array) in conjunction
with references to track parents ...

    // return data array (will contain a nested structure)
    $data = array();

    $parents = array();
    while(!empty($rows)) {
        foreach ($rows as $key => $row) {
            // add some tree related elements with default values.
            $row['selected']      = false;
            $row['childselected'] = false;
	    $row['children']      = array();

            if (!$row['parentid']) {
                $dCount          = count($data);
                $data[ $dCount ] = $row;
                $parents[ $row['nodeid'] ] =& $data[ $dCount ];
            } else if (isset($parents[ $row['parentid'] ])) {
                $dCount          = count($parents[ $row['parentid'] ]['children']);
                $parents[ $row['parentid'] ]['children'][ $dCount ] = $row;
                $parents[ $row['nodeid'] ] =& $parents[ $row['parentid'] ]['children'][ $dCount ];
            }
            unset($rows[$key]);
        }
    }

Marcus wrote:
> Hello
> 
> I have a soap call that returns something like;
> 
> Result from a print_r();
> 
> stdClass Object ( [getCategoryTreeReturn] => Array ( [0] => stdClass Object
> ( [iId] => 1 [sName] => Cars & Motorbikes [iParentId] => 0 [iTreeCount] =>
> 114302 [iLocalCount] => 0 [aSubCats] => Array ( [0] => stdClass Object (
> [iId] => 2 [sName] => Car Electronics [iParentId] => 1 [iTreeCount] => 0
> [iLocalCount] => 0 ) [1] => stdClass Object ( [iId] => 5 [sName] => Car
> Accessories [iParentId] => 1 [iTreeCount] => 57160 [iLocalCount] => 57142 )
> [2] => stdClass Object ( [iId] => 16 [sName] => Motorcycles [iParentId] => 1
> [iTreeCount] => 11 [iLocalCount] => 0 )
> 
> I need to convert that to a multidimensional array to link ID's to Parent
> ID's. I have created a function which recursively scans the result variable.
> It writes the rows to a temporary file for later fetching, but it is
> somewhat slow. Can i maintain those recursive seek to an array. As function
> is being called inside and inside, i can not return a proper value. Is there
> any way to maintain those rows without writing to tmp.
> 
> 
> The function is :
> 
> --- BEGIN ---
> 
> function deepseek($val) {
> 
> foreach ($val as $key => $value) {
> 
> 	if (is_object($value)) {
> 	$newarr = get_object_vars($newarr);
> 		foreach ($newarr as $key2 => $value2) {
> 		deepseek($value2);
> 		}
> 	}
> 
> 	if (is_array($value)) {
> 
> 		foreach ($value as $key2 => $value2) {
> 		deepseek($value2);
> 		}
> 	}
> 
> 	if (!is_array($value) AND !is_object($value)) {
> 
> 		if ($key == "iParentId" OR $key == "sName" OR $key == "iId") {
> 
> 		$output .= "$value-";
> 
> 		}
> 
> 		if ($key == "iTreeCount") {
> 		$handle = fopen("tmp/tmp","a");
> 		fwrite($handle,$output."\n");
> 		fclose($handle);
> 
> 		unset($output);
> 
> 		}
> 
> 
> 	}
> 
> }
> 
> 
> }
> --- END ---
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux