2

I am using arra_diff() to remove any matching element. but the returned results contains key and values. I am only interested in the values and not the keys.

below is the output:

array 1:
[5,7,11,14,15,22,23,24]

array 2:
[19,7]

Result (I dont like this):
{"0":5,"2":11,"3":14,"4":15,"5":22,"6":23,"7":24}

I need:
[5,11,14,15,22,23,24]

mycode

$array_1 = query->get()->pluck('id')->toArray();
$array_2 = query->get()->pluck('id')->toArray();
$result  = array_diff($array_1, $array_2);
return $result

2 Answers 2

7

You can use array_values to get it. Try this,

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

Comments

5

Every laravel query returns in result a Laravel Collection.

As Documentation of laravel say, you have a diff method that will help.

The diff method compares the collection against another collection or a plain PHP array based on its values. This method will return the values in the original collection that are not present in the given collection.

So in your case you need just to do:

$array_1 = $query->get()->pluck('id');
$array_2 = $query->get()->pluck('id');
$result  = $array_2->diff($array_1);
return $result->values();

in result we will use another method values to get just values of collection...

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.