37

What is the fastest way to convert a simple array to an associative array in PHP so that values can be checked in the isset($array[$value])?

I.e. fastest way to do the following conversion:

$array = array(1, 2, 3, 4, 5);
$assoc = array();

foreach ($array as $i => $value) {
        $assoc[$value] = 1;
}
dreftymac
  • 31,404
  • 26
  • 119
  • 182
user773755
  • 453
  • 1
  • 6
  • 11

5 Answers5

41

Your code is the exact equivalent of:

$assoc = array_fill_keys(array(1, 2, 3, 4, 5), 1); // or
$assoc = array_fill_keys(range(1, 5), 1);

array_flip(), while it may work for your purpose, it's not the same.

PHP ref: array_fill_keys(), array_flip()

Joan
  • 659
  • 2
  • 7
  • 20
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
22

If anyone is still wondering how to do this, there is an easier solution for this by using the array_combine function.

$array = array(1, 2, 3, 4, 5);
$assoc = array_combine($array,$array);
A.M.N.Bandara
  • 1,490
  • 15
  • 32
21

array_flip() is exactly doing that:

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be flipped.

If a value has several occurrences, the latest key will be used as its values, and all others will be lost.


But apart from that, there is only one type of array in PHP. Even numerical ("simple", as you call it) arrays are associative.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • array_flip will do it ...thanks.. but isset(array[5]) is not set but on an associative array isset(assoc_array[5]) is set...because the value is the key ...but in simple array the array index is the key ...am I missing something – user773755 May 27 '11 at 14:29
  • @Rome: You are mixing two things here. Every array is associative. Of course `$array[5]` is not set, because the keys go from `0` to `4`. You want an array where the keys go from `1` to `5` (or in other words, you want to set the values of the on array as keys of the other). But still, both arrays are associative. The keys of `$array` are just starting from `0` and continuous integers. – Felix Kling May 27 '11 at 14:33
  • note: array_fill_keys is some how faster than array_flip with my limited testing – user773755 Jun 01 '11 at 20:59
0

Simply use this logic

$var1 = json_encode($arr1, JSON_FORCE_OBJECT);
$var1 = json_decode($var1);

where $arr1 is the array that has to be converted to associative array. This can be achieved by json_encode and the json_decode the same

Thyagi
  • 190
  • 1
  • 6
-1
function simple_to_associative($array) {
    $new_array = [];
    $i = 0;
    $last_elem = end($array);
    $nr_elems = count($array);
    foreach ($array as $index=>$value) {
        if($i % 2 == 0 && $last_elem == $value) {
            $new_array[$value] = '';
        } elseif($i % 2 == 0) {
            $new_array[$value] = $array[$index + 1];
        }
        $i++;
    }
    return $new_array;
}

Would work on any simple array of unlimited elements.

Adrian
  • 2,273
  • 4
  • 38
  • 78