I'm writing a recursive function which iterates through a data array containing indexed and associative arrays. When an associative array of the form ['name->i,'value'->j] meets certain criteria I want to add an additional key/val pair to the array ('insert'->1).

Further iterations of updateDB() call fieldSetForTable($key,$tableArrayRef) which recursively checks the data array, starting from reference $tableArrayRef for arrays containing the same key value as $key. If found, and the ['insert'] key/val is set, the function returns true.

The problem is, as coded below, the 'insert' keys do no tappear in the array structure until updateDB() has finished it's recursion. I need the 'insert' data to be added immediately so it is available to further iterations and recursions of updateDB().

I know this is possible by passing reference vars but can't seem to get it to work!

Many thanks for any help you can give. Code below

Matt

 	function updateDB(&$data_array,$sql,$tableName,$tableArrayRef){

	foreach($data_array as $key=>$value){

		// update our array pointer for a new db table
		if($this->conf->get('dbTagToTable.'.$key)){
			$tableName = $this->conf->get('dbTagToTable.'.$key);
			$tableArrayRef = $data_array[$key];
		}											

		if(is_array($data_array[$key]) && isset($data_array[$key]['value'])){
			$dbfields = $this->fieldsInTable($tableName);

			// if the field exists in the current table... 
			if(in_array($key,$dbfields)){

				// and not been populated...
				// returns true if ['insert'] has already been set to 1
				// for any assoc value in $tableArrayRef for key $key
				if(!@$this->fieldSetForTable($key,$tableArrayRef)){

					/****** This value is set on completion of updateDB *****/
					/****** but is not immediately avail to fieldSetForTable()  *****/
					/****** while updateDB() is recursing - I want it to be!  *****/
					$data_array[$key]['insert']=1;						
				}
			}

		} else if(is_array($data_array[$key][0])){
			// Other code here...								

		}else {
			// Other code here...
		}	

	}
	return //some sql here;			
}

    Just an idea (I might be wrong here). The problem might lie in the fact that you are using foreach and maybe the first call to the function has laready created it's list of array elements to evaluate, and therefore, even though you are adding elements to the array, when the call returns to the initial call, the new elements are ignored...

    Have you tried using current() and next()? give it a go, it might solve your issue.

      Cheers for that, interesting thought. In the end I broke out the code into two separate functions. One that extracts the key/vals I want into a new array and a second one which parses this into SQL.

        Write a Reply...