Unique field value - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Unique field value.php

  1. <?php
  2.  
  3. $array = [
  4.         ['code' => '2',
  5.         'name' => 'TESTA'],
  6.         ['code' => '2',
  7.         'name' => 'TESTB'],
  8.         ['code' => '1',
  9.         'name' => 'TESTC']
  10. ];
  11.  
  12. $unique = uniqueKey($array, 'code');
  13. var_dump($unique);
  14.  
  15. function uniqueKey($array, $keyName)
  16. {
  17.     $newArray = [];
  18.     foreach ($array as $value) {
  19.         if (!isset($newArray[$value[$keyName]])) {
  20.                 echo "KEY NAME " . $value[$keyName] . "\n";
  21.             $newArray[$value[$keyName]] = $value;
  22.         }
  23.     }
  24.     return array_values($newArray);
  25. }
File Description
  • Unique field value
  • PHP Code
  • 13 Apr-2022
  • 491 Bytes
You can Share it: