[FX.php List] How do we perform the equivalent of multiple find requests?

Steven Thoms sthoms at wavecomm.com
Thu May 3 12:00:54 MDT 2012


There is an issue here:
<php manual>
array array_merge ( array $array1 [, array $... ] )
Merges the elements of one or more arrays together so that the values  
of one are appended to the end of the previous one. It returns the  
resulting array.

If the input arrays have the same string keys, then the later value  
for that key will overwrite the previous one. If, however, the arrays  
contain numeric keys, the later value will not overwrite the original  
value, but will be appended.

Values in the input array with numeric keys will be renumbered with  
incrementing keys starting from zero in the result array.

</php manual>

1. The array keys in the fx 'data' array, may be interpreted as  
numbers; they are not returned as quoted strings.

2. If not, a record returned in both arrays may have been modified, so  
the key in 'data' would be different for the same record. Small chance  
but possible.

It seems to me the only proper way to do this would be to run both  
queries and then iterate over the second creating a final array using  
the primary key of the table as the key to the resulting array, then  
adding additional records from the first. (Using the second array  
first solves the modification issue).

Something like:

$find1['data'];

$find2['data'];


foreach ( $find2['data'] as $key -> $record ) {

	$finalArray[$record['primaryKey'][0]] = $record;

}


foreach ( $find1['data'] as $key -> $record ) {

	if ( !array_key_exists($record['primaryKey'][0], $finalArray ) ) {

		$finalArray[$record['primaryKey'][0]] = $record;

	}

}

This is expensive, but would be sure to yield accurate results. In the  
end, I am with Beverly on this and would do a broader search, limiting  
responses in a single loop on the receiving end.


Steve


On May 3, 2012, at 1:30 PM, Dale Bengston wrote:

> Funny, I was just doing this exact thing yesterday.
>
> I've accomplished this using both methods. At present, I'm leaning  
> away from pulling more data than "belongs" to the user and then  
> filtering it. I'm back to doing multiple queries and merging the  
> results.
>
> Once I have the results of multiple queries, it's a simple matter to  
> "add" the data arrays to merge them. This works great, since it  
> eliminates duplicates if your found sets overlap at all.
>
> $find1 = [result of fx query 1]
> $find2 = [result of fx query 2]
>
> $finalData['data'] = $find1['data'] + $find2['data'];
>
> Poof!
>
> Two caveats:
>
> * Get the combined found set by doing count($finalData['data']). In  
> case there were dupes that got merged, you can't rely on just doing
> 	$finalData['foundCount'] = $find1['foundCount'] +  
> $find2['foundCount'];
>
> * Adding an empty array to an array can cause complaints - check for  
> a foundCount > 0  in each array first.
>
> Hope this helps,
> Dale
>
> On May 3, 2012, at 5:52 AM, Beverly Voth wrote:
>
>> If I think the foundset isn't too large, I've pulled what I can  
>> with one request and FILTER ( with switchcase or if/elseif/else )  
>> in the output loop.
>>
>>
>> -- sent from my iPhone4 --
>> Beverly Voth
>> --
>>
>> On May 3, 2012, at 3:10 AM, Malcolm Fitzgerald <malcolm at notyourhomework.net 
>> > wrote:
>>
>>> I want to combine two searches, both of which have multiple  
>>> criteria. In filemaker itself it would be easy, two find requests.
>>>
>>> I want to find all records where  "id = a AND date_1=a ... b" OR  
>>> "id=a AND date_2=a...b".
>>>
>>> Is the solution to do two separate searches and combine the  
>>> results or is there a better way?
>>>
>>> Malcolm_______________________________________________
>>> FX.php_List mailing list
>>> FX.php_List at mail.iviking.org
>>> http://www.iviking.org/mailman/listinfo/fx.php_list
>> _______________________________________________
>> FX.php_List mailing list
>> FX.php_List at mail.iviking.org
>> http://www.iviking.org/mailman/listinfo/fx.php_list
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>



More information about the FX.php_List mailing list