Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fastest way to get parent array key in multidimensional arrays with php

what is the best way to get parent array key with multidimensional arrays? for example I have this array:

array(

    [0] => array(0=> sample, 1=>picture, 2=>frame, 3=>google)

    [1] => array(0=> iphone, 1=>orange, 2=>love, 3=>msn)

    [2] => array(0=> joe, 1=>geee, 2=>panda, 3=>yahoo)
)

now I need to search for example google and get the parent array key.. which it should be 0...any ideas? I used for loop for this but I think it will be slow if I have arrays with 700000 rows..

like image 774
Desolator Avatar asked Dec 08 '22 03:12

Desolator


1 Answers

If you have an array with 700,000 rows you are almost certainly doing something wrong... I would first recomend thinking about utilizing a different data store: flat file or some type of DB.


foreach($array as $key => $value) {
    if(in_array('google', $value)) return $key
}

like image 118
mmattax Avatar answered May 21 '23 07:05

mmattax