1

I have a string that I explode to an array:

$array = explode(" ", $string);

Which gives me an array like this:

array:4 [▼
  0 => "Employee"
  1 => "Contract"
  2 => "John" // first name
  3 => "Smith" // last name
]

I need to match the user by the first_name and last_name in the array. However the first and last names are not always in this oder in the array.

So something like:

$user = User::where('first_name', 'LIKE', "%{$array}%")->where('last_name', 'LIKE', "%{$array}%")->first();

This give me an Array to string conversion exception.

So I'm stuck trying to get the right query to match the names in my array with the names in my user.

1
  • If the first name and last name are not in order, then the further actions to be taken doesn't make sense to me. They have to be in order. You could tweak the array and make Employee and contract come before or after but there seems to be a design mistake here making you to do these workarounds. Commented Aug 3, 2019 at 19:43

3 Answers 3

2

If your array is always the same (key 2 always === first_name and key 3 always === last_name), you can use array notation to stop the error:

$user = User::where('first_name', 'LIKE', "%".$array[2]."%")->where('last_name', 'LIKE', "%".$array[3]."%")->first();

But you've said that the keys are specifically NOT always the same. You will need to write something to normalize the data in advance if this is the case. If there are always a 'type' like 'employee' and a limited number of choices for what this might be, you can eliminate based on a match to the type:

 foreach($array as $key=>$val){
    if(!in_array($val, $yourTypes)
         // set the value to first or last depending on the order these come in

Or, if there is some way to standardize the array before it gets to the User pull, you should try to do this to make sure you know which value is hitting the query. Something like a common input format for a CSV file - users must set columns in an exact order - if you can correct for this, you can solve it. If there is ability to create a named key, this would also solve it, though it looks like that's not possible based on your pull from a simple string. A more radical step would be to have a mapping program for an admin to match each string manually: I.E. each string is exploded and displayed on the screen with each key number and the corresponding value, admin can choose which key is first_name, which key is last_name.

But to try to pull based on multiple different array structures where it is unknown, is likely not possible. This is why APIs will typically have specifically defined keys and output - not knowing this puts the burden on a manual intervention like the mapping thing I noted above.

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

2 Comments

Very well put... exactly what I'm struggling with. Unfortunately the only things that will always be in the array will be the first and last name. The other info can vary. And of course the names will be different. What if I leave it as a string "Employee Contract John Smith" or could be "Info Smith John Vendor ". Again I would need to match user with the first_name John, and last_name Smith.
Yeah, you are probably not going to be able to do this automatically based on different data coming in. Your only solution might be to implement the admin manual mapping thing or similar to what I noted above. Not an easy solve unfortunately.
0

If all the time you have on the position 0 and 1 you will have "Employee" and "Contract" you can use something like this:


unset($array[0], $array[1]);

$array = $array = array_values($array);

$user = User::where('first_name', 'LIKE', "%{$array[0]}%")->where('last_name', 'LIKE', "%{$array[1]}%")->first();

Comments

0

You can use list to convert the exploded array strings into variables. ,, ignores the first two.

list(,, $fname, $lname) = explode(' ', $string);


These become declared variables which can be used like so:

User::where('first_name', 'LIKE', "%{$fname}%")
    ->where('last_name',  'LIKE', "%{$lname}%")
    ->first();

Live Demo at 3v4l.

3 Comments

Thanks that does work however this assumes the info in the array with be in the same order every time. Unfortunately in my case it is not.
Then thats a design flaw you cannot fix without changing your design
Yeah, not my design. It's a legacy db where they put this info in a description field.

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.