Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply search on Multidimensional array in Laravel

Tags:

php

laravel

How can I search through multiple arrays. I am making custom search functionality for datatable. How can I do that? I am stuck how can I search and find amongst array and return the array.

$records = Array
(
    [0] => Array
        (
            [0] => 28
            [1] => http://198.58.98.34:4600/uploads/item/priority_images-1519195620005Item1.png
            [2] => Currency
            [3] => Demo1
            [4] => Hello
            [5] => 980
            [6] => 2018-02-21T06:47:15.264Z
        )

    [1] => Array
        (
            [0] => 27
            [1] => http://198.58.98.34:4600/uploads/item/1519120091372-filewinter_is_here_game_of_thrones_hd-1920x1080.jpg
            [2] => coins212
            [3] => Demo2
            [4] => Hello1
            [5] => 54564
            [6] => 2018-02-21T06:38:04.053Z
        )
);

$search = $request['search']['value'];

I tried using array_column but it was not working. How Can I search and return the searched data.?

like image 276
Bits Please Avatar asked Oct 23 '25 15:10

Bits Please


1 Answers

You can use collect function in laravel to make collection and the use where function to filter

$collect =  collect($records);
$search = $collect->where('search_column','value'); 

In your example you can use column number as. ie 3rd column which key is 2

$search = $collect->where('2','Currency');

Will give the array which have Currency in third columnd ie. with key 2

Please check all available method for collection here

like image 152
Niklesh Raut Avatar answered Oct 25 '25 04:10

Niklesh Raut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!