Paul Iverson Cortez
Paul Iverson Cortez

Reputation: 321

PHP: How to search in array of objects and return the res

enter image description here

I have this Array of Objects and I want to search a string to all of the keys and return all the data of the matched objects. I don't know if there's a duplicate of this question. hope you guys can save my day.

the photo below is my code. I don't use eloquent btw and the data is from call-in SQL.

enter image description here

Below is the UI that I made. enter image description here

Upvotes: 1

Views: 387

Answers (2)

Bas K
Bas K

Reputation: 41

You want to use the pluck method from laravel(eloquent)

So something like this:

$plucked = $paginatedItems->pluck('item_code', 'unit_measure');

$plucked->all();

See: https://laravel.com/docs/8.x/collections#method-pluck

Upvotes: 2

James P
James P

Reputation: 101

Without seeing your code, it's difficult to determine which approach would be best. However, there are a few ways of doing this. Below are some untested examples. But please use them as examples as there are far easier and better ways of doing this. I am simply trying to point you in the right direction.

$array = array_search([SEARCH TERM], array_keys([YOUR ARRAY]));
$results = [];
$array_keys = array_keys([YOUR ARRAY]);
for($i = 0; $i < count($array_keys); $i++) {
    if($array_keys[$i] == "[SEARCH TERM]") {
        $results[] = $[YOUR ARRAY][$i];
    }
}

Also try: php search array key and get value

Edit: this is pure PHP - as you are using laravel, there are definitely better ways of doing this. Please read the documents for pluck

Upvotes: 0

Related Questions