php: how to get associative array key from numeric index?

You don’t. Your array doesn’t have a key [1]. You could: Make a new array, which contains the keys: $newArray = array_keys($array); echo $newArray[0]; But the value “one” is at $newArray[0], not [1]. A shortcut would be: echo current(array_keys($array)); Get the first key of the array: reset($array); echo key($array); Get the key corresponding to the … Read more

Hash tables VS associative arrays

In PHP, associative arrays are implemented as hashtables, with a bit of extra functionality. However technically speaking, an associative array is not identical to a hashtable – it’s simply implemented in part with a hashtable behind the scenes. Because most of its implementation is a hashtable, it can do everything a hashtable can – but … Read more

PHP combine two associative arrays into one array

array_merge() is more efficient but there are a couple of options: $array1 = array(“id1” => “value1”); $array2 = array(“id2” => “value2”, “id3” => “value3”, “id4” => “value4”); $array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/); $array4 = $array1 + $array2; echo ‘<pre>’; var_dump($array3); var_dump($array4); echo ‘</pre>’; // Results: array(4) { [“id1”]=> string(6) “value1” [“id2”]=> string(6) “value2” [“id3”]=> … Read more

How to make a list of associative array in yaml

Your YAML looks okay, or you can configure an array of hashes like this : content_prices: – country: AU price: 6990000 – country: AT price: 4990000 – country: BE price: 4990000 Which will load as the following hash: {“content_prices”=>[ {“country”=>”AU”, “price”=>6990000}, {“country”=>”AT”, “price”=>4990000}, {“country”=>”BE”, “price”=>4990000}]} But that still doesn’t give you any reference to the … Read more

Is there a way to create key-value pairs in Bash script?

In bash version 4 associative arrays were introduced. declare -A arr arr[“key1”]=val1 arr+=( [“key2”]=val2 [“key3”]=val3 ) The arr array now contains the three key value pairs. Bash is fairly limited what you can do with them though, no sorting or popping etc. for key in ${!arr[@]}; do echo ${key} ${arr[${key}]} done Will loop over all … Read more