Reputation: 395
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
Upvotes: 0
Views: 1176
Reputation: 20371
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.
Upvotes: 4