[Solved] PHP add counter to name if already exists in array
$arrayOfNames = array( array(‘clientName’ => ‘John’), array(‘clientName’ => ‘John’), array(‘clientName’ => ‘Mary’), array(‘clientName’ => ‘Mary’), array(‘clientName’ => ‘Mary’), array(‘clientName’ => ‘Tony’), array(‘clientName’ => ‘Alex’) ); $namesCount = array(); $finalNames = array_map(function ($item) use (&$namesCount) { if (!isset($namesCount[$item[‘clientName’]])) { $namesCount[$item[‘clientName’]] = 0; } $namesCount[$item[‘clientName’]]++; $item[‘clientName’] = $item[‘clientName’] . ‘ ‘ . $namesCount[$item[‘clientName’]]; return $item; }, $arrayOfNames); … Read more