Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Laracast13's avatar

php array search partial match

Hello

Have array

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
            [0] => 1
            [1] => 
            [2] => 42U703T6319
            [3] => 23GRT3273240 
        )

    [2] => Array
        (
            [0] => 2
            [1] => 
            [2] => 44AAD602383
            [3] => 21873TT9996 
        )

    [3] => Array
        (
            [0] => 3
            [1] => 
            [2] => 223MSK34345
            [3] => 21428739996 
        )
)

Using this code search exact match

     $positionInStr = array_search(223MSK34345, Arr::dot($myArray));
     $arrayKeys = Str::of($positionInStr)->explode('.');
     $k = $arrayKeys[0];
     $myArray = $myArray[$k];

how search partial match

0 likes
16 replies
Laracast13's avatar

@tisuchi For ex. in some case array have such entry

[2] => 'text text 223MSK34345'

so if I need sarch 223MSK34345 array_search can not find This code ' 223MSK34345 ' always end of sting . I need search partial match or last part string

Sinnbeck's avatar

@www888 ok something like (untested but I think it should work)

$item = collect($arr)->first(function($items) use ($search) {
    foreach ($items as $item) {
         if (Str::contains($item, $search)) {
               return true;
        } 
   } 

  return false;
});
Laracast13's avatar

@Sinnbeck

$arr attach array , $search ="223MSK34345"

getting error : explode(): Argument #2 ($string) must be of type string, Closure given

Laracast13's avatar

@Sinnbeck

project\vendor\laravel\framework\src\Illuminate\Collections\Traits\EnumeratesValues.php: 321

   public function firstWhere($key, $operator = null, $value = null)

    {

        return $this->first($this->operatorForWhere(...func_get_args()));  //321 line

    }
Sinnbeck's avatar

@www888 do you mean you need the index of the outer array?

$index = collect($arr)->search(function($items) use ($search) {
    foreach ($items as $item) {
         if (Str::contains($item, $search)) {
               return true;
        } 
   } 

  return false;
});
Laracast13's avatar

@Sinnbeck Yes my goal is find array where 223MSK34345 and then access value to display each in array 3

[3] => Array
    (
        [0] => 3
        [1] => 123
        [2] => 223MSK34345
        [3] => 21428739996 
    )
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@www888 not sure why you can't use my original example? Did you try it?

$item = collect($arr)->first(function($items) use ($search) {
    foreach ($items as $item) {
         if (Str::contains($item, $search)) {
               return true;
        } 
   } 

  return false;
});
dd($item); //gives you all results in index 3 
1 like
jdc1898's avatar

I think there is a function dedicated for just this purpose, preg_grep. It should take a regular expression as first parameter, and an array as the second.

Please or to participate in this conversation.