1

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

8
  • can you provide a search example? Commented Jun 23, 2021 at 8:38
  • 1
    for example, i search for "s" and I want to match it to all the value of the key like a loop in all of the key-value pairs and return all the key-value pairs that have a matched to "s" Commented Jun 23, 2021 at 8:40
  • and then you get for example: 132 => unit_measure, description? because the contain an 's' in the key name? Commented Jun 23, 2021 at 8:41
  • 1
    Yes, you're correct Commented Jun 23, 2021 at 8:43
  • 1
    all the properties sir. Commented Jun 23, 2021 at 8:47

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer I appreciate it so much. unfortunately, I am not using eloquent sorry I didn't clarify it in my question.
this answer is not relevant for the question
As far as I know the LenghtAwarePaginator is just a collection You can use pluck on the collection as well
0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.