PHP - Detecting duplicate values in a nested array -
i'm using api returns json output in php.
php
$result = $api->sendrequest("getusers", $inputparameters); $output = json_decode($result, true); an illustration of array returned api. can print out specific field values fine, can't figure out how write simple if statement indicates whether or not there duplicate names within query result, duplicate [fullname] fields seen below.
array ( [status] => array ( [request] => getusers [recordstotal] => 3 [recordsinresponse] => 3 ) [records] => array ( [0] => array ( [fullname] => smith, tom [firstname] => tom [lastname] => smith ) [1] => array ( [fullname] => jones, bill [firstname] => bill [lastname] => jones ) [2] => array ( [fullname] => smith, tom [firstname] => tom [lastname] => smith ) ) ) any help appreciated.
not tested, maybe seek like
function dupecheck($array, $attribute = 'fullname') { $list = array(); foreach($array['records'] $value) { if(in_array($value[$attribute], $list)) homecoming true; $list[] = $value[$attribute]; } homecoming false; } just iterating on records, maintain list of values of whatever attribute, 1 time finds 1 in array, returns true.
then just:
if(!dupecheck($output, 'fullname')) { // no dupes in api response } php arrays json
No comments:
Post a Comment