A quick way to get the parent key of an array in multidimensional arrays with php

What is the best way to get a parent array 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 look for a google example and get the parent array. which should be 0 ... any ideas? I used to loop for this, but I think it will be slow if I have arrays with 700,000 rows ..

+4
source share
2 answers

If you have an array with 700,000 rows, you are almost certainly doing something wrong ... I would first advise you to think about using another data store: a flat file or some kind of database.

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

Arrays with 700,000 lines? How many arrays? The problem 9/10 times is that your data is incorrectly installed.

I am going to go ahead and assume that you are doing some sort of search. Since you cannot index the array (in the index search value), you are probably the best place the data in the database and search for column indexing as quickly as possible.

Depending on the context, you might also consider saving your data in files, one per array and using a file search to find which file contains your value.

+4
source

Source: https://habr.com/ru/post/1308937/


All Articles