Get only the raw values of an array in Laravel Php after using array_diff()

318
May 12, 2017, at 10:43 PM

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
Answer 1

You can use array_values to get it. Try this,

array_values($result);
Answer 2

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...

READ ALSO
Convert C# Hash Code to PHP for REST API Connection

Convert C# Hash Code to PHP for REST API Connection

Below is an excerpt from documentation for connecting to an APII need to make this connection with PHP, but I am coming up short

279
Disable default Sonata success message

Disable default Sonata success message

I'm working with Sonata Admin Bundle and I'm trying to remove or rewrite default message for success: "Item X has been successfully created"

255
Allow access to “Deny from all” directory

Allow access to “Deny from all” directory

What I want to happen is:

144
how to check image file authenticity through PHP

how to check image file authenticity through PHP

i have written a php script which checks the image file for its extensionsuch as JPG, JPEG, PNG, GIF uploaded thru an HTML form

223