I want to remove rows that have a duplicate value in one key of an associative array in…
- User
I want to remove rows that have a duplicate value in one key of an associative array in PHP
Assistant
php<?php $array = array( array('id' => 1, 'name' => 'John Doe'), array('id' => 2, 'name' => 'Jane Doe'), array('id' => 1, 'name' => 'John Smith'), array('id' => 3, 'name' => 'Mary Johnson'), ); $uniqueArray = array(); $seen = array(); foreach ($array as $row) { if (!in_array($row['id'], $seen)) { $uniqueArray[] = $row; $seen[] = $row['id']; } } print_r($uniqueArray);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.