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

与世无争的帅哥 提交于 2020-01-01 13:01:06

问题


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..


回答1:


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
}




回答2:


Arrays with 700,000 rows? How many arrays? 9/10 times problem is that you've got your data set up wrongly.

I'm going to go ahead and assume you're doing a search of some sort. As you can't index an array (in the search meaning of index) then you're probably best putting the data into a database and making the most of column indexing to search fast.

Depending on context, you may alternatively want to think about storing your data in files, one per array, and using file searches to find which file contains your value.



来源:https://stackoverflow.com/questions/2776107/fastest-way-to-get-parent-array-key-in-multidimensional-arrays-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!