I want to set the value of an associative array using the array index of the key/value pair. For example:
$my_arr = array( "bling" => "some bling", "bling2" => "lots O bling" ); $my_arr[1] = "not so much bling"; // Would change the value with key bling2.
How can this be accomplish this without using the key string?
You can use the PHP array_values() function to get all the values of an associative array.
PHP arrays can contain int and string keys at the same time as PHP does not distinguish between indexed and associative arrays.
Associative arrays differ from normal, fixed-size arrays in that they have no predefined limit on the number of elements, the elements can be indexed by any tuple as opposed to just using integers as keys, and the elements are not stored in preallocated consecutive storage locations.
We can get the array index by using the array_search() function. This function is used to search for the given element.
Use array_keys.
$keys = array_keys($my_arr); $my_arr[$keys[1]] = "not so much bling";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With