1

I have a big array (10M of strings). I have to get value by key or by value many times.

To get by key, I use isset => O(1) => much faster.

To get by value, I use in_arrayor => O(n) => more slowly, because it does a linear search though the array until it finds the value.

In PHP, Is it possible to set key with more items and do optimize search on kthis key?

For example :

$bigArray = ((0, "s0") => true, 
             (1, "s1") => true, 
             (2, "s2") => false, 
              ...., 
             (1000000, "s1000000") => true);
$Test2 = get_optimize_function("s2", $bigArray); // Key = (2, "s2"), Value = false
$Test1 = get_optimize_function(1, $bigArray);    // Key = (1, "s1"), Value = true
LeMoussel
  • 5,290
  • 12
  • 69
  • 122
  • so, your key is array? – Iłya Bursov May 04 '16 at 14:59
  • No. In PHP array keys must be uniqe and it should be a string or an integer. I think something wrong with the design of the code. If it comes from database, then you need to narrow your results from the query. If not, then another alternative, if you flip the keys/values, so `true => [key1, key2, key3]`. Anyway, you sad, you need to search value sometimes, so it won't help you. – vaso123 May 04 '16 at 14:59
  • @lolka_bolka are you sure that php array keys should be strings? – Iłya Bursov May 04 '16 at 15:00
  • @Lashane as I sent my comment, I immediatly removed that :) thx anyway, of course, it could be integer too. http://stackoverflow.com/questions/10696067/characters-allowed-in-php-array-keys – vaso123 May 04 '16 at 15:01
  • make key as '0|s0', '1|s1'.... – splash58 May 04 '16 at 15:03
  • @splash58, '0|s0', '1|s1' is a string, but how can i do an optimize search on '0', '1', 's0', 's1' ? – LeMoussel May 04 '16 at 15:06
  • @LeMoussel I'm not sure why you ignored my question, lets repeat - is your key scalar value (as you explained) or array (as in yours example)? – Iłya Bursov May 04 '16 at 15:19
  • @Lashane key is scalar value with a string and an integer.(0 to 1M) – LeMoussel May 04 '16 at 15:21
  • @LeMoussel scalar value is either string or integer, you're describing array or struct or pair, which is not scalar value, with your current structure you cannot get boolean value from this array neither with integer nor string, for both your use cases you need to do full array scan – Iłya Bursov May 04 '16 at 15:34
  • @Lashane But it's maybe string, like splash58 say, with '0|s0', '1|s1', .. But I don't know how to do an optimize search on '0', '1', 's0', 's1' – LeMoussel May 04 '16 at 15:36
  • @LeMoussel maybe you need 2 separate arrays, one where keys are integers, values are boolean, second where keys are string and values are boolean, then you will be able to perform both searches in avg O(1) – Iłya Bursov May 04 '16 at 15:37
  • 1
    Maybe it would be useful to make additional array s0=>key0... – splash58 May 04 '16 at 15:40
  • @Lashane I tested this, but it consume more memory. I can't do this. – LeMoussel May 04 '16 at 15:40
  • 2
    @LeMoussel then I'd say - its impossible, either you have fast access (but spending more memory for special structures) or you have minimal memory footprint (with flat array) but slow access time, maybe you can provide more details of real problem, so we can find trade off – Iłya Bursov May 04 '16 at 15:44

0 Answers0