Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to Find Unique / Distinct Values in an Array?

Featured Replies

I use FX.php, FM7, FMS7A, PHP 5.1.2 running on IIS.

How can I use PHP to extract a found count for unique values from single column?

Here is a mythical record set.

larry, banana

johnny, apple

sally, banana

bobby, pear

billy, mango

I'd like to find the number of unique fruits (4).

One option is to order the record set and then run looping logic to disregard duplicates. This seems inefficient.

How might I accomplish this using FX.php? Any ideas on using arrays / associative arrays?

Thank you,

Shannon

You may want to look at some of the array functions at www.php.net

1 that springs to mind is array_count_values()

Details of the function can be found at php.net

  • Author

Thanks Andy. That is probably the final step in the process.

I first need to figure out how to get all the values of a column of multiple records into a single array. I've found basic examples all over the net, but haven't figured out how to do it with an array returned via FX.php. I think my answer is somewhere in the realm of Associative arrays. (?)

Any other ideas? I'll post my solution if I can figure it out.

Shannon

-=-=-

  • Author

I figured it out. I'll continue my people/fruit example:

<?

$Search = new FX($serverIP,$webCompanionPort);

$Search->SetDBData('FMPFile.fp7','LayoutName');

$Search->SetDBPassword($webPW,$webUN);

$Search->AddDBParam('Fruit',' * ');

$SearchResult=$Search->FMFind();

$FoundCount = $SearchResult['foundCount'];

?>




This would return my mythical record set. 

larry, banana

johnny, apple

sally, banana

bobby, pear

billy, mango





I studied the result of print_r($SearchResult); to learn how FX.php delivers XML produced by FileMaker. From there, I used a loop to build a new array. The new array is built on a single 'column' from the $SearchResult array.




<?

foreach($SearchResult['data'] as $SearchKey=>$SearchRecord) { 

	$FruitArray[] = $SearchRecord['Fruit'][0];

}

?>

Now I can pimp out my new array as needed...

$FruitArray;

would result:

Array (

[0] => banana

[1] => apple

[2] => banana

[3] => pear

[4] => mango

)

$FruitUnique = array_unique($FruitArray);

would result:

Array (

[0] => banana

[1] => apple

[2] => pear

[3] => mango

)

$ValuesPerFruit = array_count_values($FruitArray);

would result:

Array (

[banana] => 2

[apple] => 1

[pear] => 1

[mango] => 1

)

$FruitCount = count($FruitUnique);

would result:

4

etc...

HTH,

Shannon

-=-=-

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.