Reputation: 321
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.
Upvotes: 1
Views: 387
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
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