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

5

You can use array_values to get it. Try this,

array_values($result);
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...

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.