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.
Result of php executing
Full code of Unique field value.php
- <?php
- $array = [
- ['code' => '2',
- 'name' => 'TESTA'],
- ['code' => '2',
- 'name' => 'TESTB'],
- ['code' => '1',
- 'name' => 'TESTC']
- ];
- $unique = uniqueKey($array, 'code');
- var_dump($unique);
- function uniqueKey($array, $keyName)
- {
- $newArray = [];
- foreach ($array as $value) {
- if (!isset($newArray[$value[$keyName]])) {
- echo "KEY NAME " . $value[$keyName] . "\n";
- $newArray[$value[$keyName]] = $value;
- }
- }
- return array_values($newArray);
- }