What is the more efficient way to store this ordered pair in php?

Which of the following two data structures is “better”?

array('key'=>array(1,2,3,4))

OR

array('key',array(1,2,3,4))

ie, is it better to store the array as the second element in an array of two elements or as the only element in the array with the key "key".

Suppose for my purposes, in matters of convenience, they are equivalent. I am curious if it uses more resources than another.

+3
source share
3 answers

You are using what is suitable for what you are trying to save.

If the key refers to an array of values ​​and its unique, then use the key / value.

, , - .

+4

, .

,

array(
  array('key', array(...)),
  array('key', array(...)),
  array('key', array(...)),
  etc
);

array(
  'key' => array(...),
  'key' => array(...),
  'key' => array(...),
);

, .

+2

Recognition of the key value in this is as follows:

"I want the meaning of Bob"

$bob = $myArray['bob'];

instead

foreach($myArray as $key => $value) {

    if ($value[0] === 'bob') { // the first value of your 2nd type
        $bob = $myArray[1]; // get the next value.
    }

}

Of course, for the second example you can consider end(). But compare 2, obviously the first example wins!

+2
source

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


All Articles