1

I have general data array and I need to get array of specific data inside this general array so I can match it against my database.

Code

$nums = [];
foreach($request->phones as $phone) {
    foreach($phone['_objectInstance']['phoneNumbers'] as $number) {
        $nums = $number['value'];
    }
}

$contacts = User::whereIn('phone', $nums)->get();

PS: $number['value'] is the data that I want to make array of it.

Sample data that I receive in backend

aravel

current error

Argument 1 passed to Illuminate\\Database\\Query\\Builder::cleanBindings() must be of the type array, string given, called in /home/....../vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php on line 918

exception: "TypeError"

Question

How can I make array of my numbers?

Ps: please if you know cleaner way to write this code, than my code above don't hesitate to share with me.

2 Answers 2

3

You're assigning $nums to be a new string on every iteration of the loop, rather than appending it to the array.

Just switch this line out:

$nums = $number['value'];

For

$nums[] = $number['value'];

Here are the docs for array_push(), which is the long way of writing the second line.

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

8 Comments

that fixed the error thanks but why i cant get array of my users list? $contacts I mean i should get 2 result i just get 1
You can stick a ->toArray() after your ->get() to convert it to an array. If it's only including one contact, you should check that $nums is correct, and that it correctly matches your contacts.
no have any rest, simply make array of numbers and find users who has any of those numbers
But $contacts isn't being returned or sent to the browser, so how are you looking at it in the inspector?
data of contacts i'm getting in my app (debug mode) so that screenshot i sent was result of $contacts
|
2

You are declaring $nums array, but inside the loop, you re-declaring it by a string again. Fix the array assignments like that.

$nums[] = $number['value'];

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.