2
   (
      "userServiceIds": "1,2,3",
      "enggServiceIds": "4,5,6,2,1",
   )

As In these array we need Only enggServiceIds only where It must not be repeated in userServiceIds

As my required result is : - 4,5,6 only

the code must be run in PHP

I have tried this code

$input = array("$data[userServiceIds]" , "$data[enggServiceIds]");
            $result = implode(',',$input);
            $str = implode(',',array_unique(explode(',', $result)));

but the result is :- 1,2,3,4,5,6

As my required result is : - 4,5,6 only

6
  • 1
    And where is the code that you have tried ? Commented Oct 4, 2017 at 9:32
  • 1
    I don't think it's array. It's json {}? Commented Oct 4, 2017 at 9:32
  • its first its json then its converting to array Commented Oct 4, 2017 at 9:35
  • I think you want array_diff() rather than simply array_unique() Commented Oct 4, 2017 at 9:35
  • not working array_diff() Commented Oct 4, 2017 at 9:36

3 Answers 3

2

As the input is JSON, convert it to array first. Then using array_diff

$json = '{
  "userServiceIds": "1,2,3",
  "enggServiceIds": "4,5,6,2,1"
}';

$aData = json_decode($json, true);

$aResult = array_diff(explode(',',$aData['enggServiceIds']), explode(',',$aData['userServiceIds']));

print_r($aResult);
Sign up to request clarification or add additional context in comments.

Comments

1

read up on array_diff

$userServiceIds = [1,2,3];
$enggServiceIds = [4,5,6,2,1];

var_dump(array_diff($enggServiceIds, $userServiceIds));

returns:

array(3) {
  [0]=>
  int(4)
  [1]=>
  int(5)
  [2]=>
  int(6)
}

live preview

Comments

1

solution:

$array1 = array(1,2,3);
$array2 = array(3,4,5,6);
$resultarray = array();
$resultarray = array_diff($array2, $array1);

result:

Array ( [1] => 4 [2] => 5 [3] => 6 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.