What your asking for is impossible, as when you assign the array like this:
$r1 = array('Hello','World');
PHP will assign “Hello” to the key 0 and “World” to the key 1.
With other words they will have keys assigned to them.
This means that as long as you allow integer keys to be counted as “assigned” keys, there is no way to differentiate if it was a key you set yourself, or if it was auto assigned.
In the event that you do only count string keys as assigned ones, then you can easily just check the key to see if it is a string or integer and if its an integer to use the value for key.
yeah that’s the conclusion I was at, was just wondering if there’s some method PHP has, in the unlikely event that it in fact keeps track of who defined the integer index.
That option would be more resource demanding than to set them from the start, since that would also require you to know if the key exists or not.
A more resource friendly option would be something like this.
function prepareArray(&$options, $value, $key=null) {
$options[(($key === null)?$value:$key)] = $value;
}
$options = array();
//When you assign the values you have you just do, if you are uncertain that there is a key or not
prepareArray($options, $array_value, ((empty($array_key))?null:$array_key));
//Or if your certain that the value has no key
prepareArray($options, $array_value);
//Or if your certain it has a key
prepareArray($options, $array_value, $array_key);