O
O
Outoverlay2015-12-03 11:28:49
PHP
Outoverlay, 2015-12-03 11:28:49

Is it possible to create a function like array_combine in php?

There is an array - $arr = array(1,2,3);
And you need to make these values ​​become keys, and then add more arrays to these keys.
I don't know why, but I was wondering how to create an array_combine with my own hands.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-12-03
Protko @Fesor

function my_array_combine(array $keys, array $values) {
     if (count($keys) !== count($values)) throw new \InvalidArgumentException();
     $keys = array_values($keys);
     $values = array_values($values);
     $result = [];
     for($i = 0;$i<count($keys);$i++) {
          $result[$keys[$i]] = $values[$i];
     }

     return $result;
}

O
OVK2015, 2015-12-03
@OVK2015

In foreach, iterate over the $arr array, and insert new elements into the resulting array

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question