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

Laracast13's avatar

PHP search value in array and return array key

Hello

I have like this array

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
            [0] => 1
            [1] => 
            [2] => 42U7036319
            [3] => 234BKG03273240 
        )

    [2] => Array
        (
            [0] => 2
            [1] => 
            [2] => 442MSKU4602383
            [3] => 218739996 
        )

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

need search value in array and return array key. for ex search 442MSKU4602383 return key 2

0 likes
11 replies
tisuchi's avatar

@www888 That's what you can try-

        $items =
            [
                [],
                [
                    1,
                    '',
                    '42U7036319',
                    '234BKG03273240',
                ],
                [
                    2,
                    '',
                    '442MSKU4602383',
                    '218739996'
                ],
                [
                    3,
                    '',
                    '223MSKU4636845',
                    '21428739996'
                ]
            ];


        $positionInStr = array_search('442MSKU4602383', Arr::dot($items));

        // Here is array keys
        $arrayKeys = Str::of($positionInStr)->explode('.');

        dd(
            $items[$arrayKeys[0]][$arrayKeys[1]]
        );
jlrdw's avatar

@www888 you tried the very first answer and it didn't work?

Edit

This code taken from https://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php

Does work, I just tested.

function _search_array_by_value($array, $value) {
        $results = array();
        if (is_array($array)) {
            $found = array_search($value,$array);
            if ($found) {
                $results[] = $found;
            }
            foreach ($array as $subarray)
                $results = array_merge($results, $this->_search_array_by_value($subarray, $value));
        }
        return $results;
    }

It would be easier on you if you had "named" or string keys to work with.

Above gives answer in an array however:

^ array:1 [▼
  0 => 2
]

The value in answer is the key to the search: '442MSKU4602383'

Just FYI, sometimes try a Google first, I and others here also search when we need things like array help.

Edit

I think you want which sub array it's located, so:

        $search = '442MSKU4602383';

        $subarray = false;

        foreach ($items as $k => $v)
            if (in_array($search, $v)) {
                $subarray = $k;
                break;
            }

        dd($subarray);

Give you 2 which is this:

            [
                2,
                '',
                '442MSKU4602383',
                '218739996'
            ],
1 like
tisuchi's avatar

@www888 You should use $arrayKeys that will give you found keys.

Have you tried it?

jpmg's avatar

Hello @www888 this work for me.

 		$a = Array(
        		 Array
            		(
                		'color' => "Red",
               			'age' => 22,
          		  ),
         		Array
         		(
            		'color' => "Blue",
             		'age' => 44,
               ),
 );
$thekey = array_search('Red', array_column($a, 'color'));;
dd($thekey);
Joeszeto's avatar
$foo = [
    0 => [],
    1 => [
        2 => '42U7036319',
        3 => '234BKG03273240'
    ],
    2 => [
        10 => '218739996'
    ]
];

function search($haystack, $needle) {
    foreach($haystack as $key => $value ) {
        if (is_array($value) && $result = search($value, $needle)) {
            return $result;
        }
        if ($value === $needle) {
            return $key;
        }
    }
    return false;
}
var_dump(search($foo, '234BKG03273240')); // 3
var_dump(search($foo, '218739996')); // 10

Hello @www888 this work for me

Please or to participate in this conversation.