0

I'm trying to do something like:

keys = [2,3,10]  # or even with assosiative keys like "orange", "apple", ..
vals = [7,9,11]
dict = array_combine(keys,vals)

Any way to implent that example as using array_combine() bult-in funcion of PHP5 ?

https://www.php.net/manual/en/function.array-combine.php

1
  • What is the expected output? Commented Apr 13, 2014 at 16:21

1 Answer 1

4
dict(zip(keys, vals))

Seems to be what you want.

Demo:

>>> keys = [2,3,10]
>>> vals = [7,9,11]
>>> dict(zip(keys, vals))
{2: 7, 3: 9, 10: 11}

zip puts elements at corresponding indices within the two lists into tuples. We can then put those pairs together into a new structure with the dict constructor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.