Convert an associative array to a simple array of its values in php
simply use array_values function: $array = array_values($array);
simply use array_values function: $array = array_values($array);
http://php.net/manual/en/language.types.array.php <?php $array = array( “foo” => “bar”, “bar” => “foo”, ); // as of PHP 5.4 $array = [ “foo” => “bar”, “bar” => “foo”, ]; ?> Standard arrays can be used that way.
Use an object, as people are saying. However, note that you can not have integer keys. JavaScript will convert the integer to a string. The following outputs 20, not undefined: var test = {} test[2300] = 20; console.log(test[“2300”]);
Can’t you just do: $resulting_array = $array2 + $array1; ?
You can use http_build_query() to do that. Generates a URL-encoded query string from the associative (or indexed) array provided.
Another option, if portability is not your main concern, is to use associative arrays that are built in to the shell. This should work in bash 4.0 (available now on most major distros, though not on OS X unless you install it yourself), ksh, and zsh: declare -A newmap newmap[name]=”Irfan Zulfiqar” newmap[designation]=SSE newmap[company]=”My Own Company” … Read more
You could try: dict((k, bigdict[k]) for k in (‘l’, ‘m’, ‘n’)) … or in Python 3 Python versions 2.7 or later (thanks to Fábio Diniz for pointing that out that it works in 2.7 too): {k: bigdict[k] for k in (‘l’, ‘m’, ‘n’)} Update: As Håvard S points out, I’m assuming that you know the … Read more
ECMAscript 6 introduced Object.assign() to achieve this natively in Javascript. The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. MDN documentation on Object.assign() var o1 = { a: 1 }; var o2 = { … Read more
You can do: foreach ($arr as $key => $value) { echo $key; } As described in PHP docs.
This question provides an idiom. You use one of the dicts as keyword arguments to the dict() constructor: dict(y, **x) Duplicates are resolved in favor of the value in x; for example dict({‘a’ : ‘y[a]’}, **{‘a’, ‘x[a]’}) == {‘a’ : ‘x[a]’}