I am having trouble dealing with arrays. PHP throws an error if I access an index of an array which does not exists and It is getting difficult conditioning for every index. Is there any way I could set default value for every index of an array so even if I access a non-existent index of an array then it returns the default set value?
Asked
Active
Viewed 1,899 times
0
-
Define a function to access your arrays, something like this: `function access($array, $key) { return isset($array[$key]) ? $array[$key]:'Default Value'; }` and just use `access($array, 'key')` instead of `$array['key']` – t3chguy Jul 25 '14 at 10:45
-
This is one of those things in PHP. You need to get accustomed to checking for the presence of a key that could possibly be missing because there is no syntax to avoid it without issuing an E_NOTICE – Michael Berkowski Jul 25 '14 at 10:47
-
possible duplicate of [PHP default array values if key doesn't exist?](http://stackoverflow.com/questions/9555758/php-default-array-values-if-key-doesnt-exist) – Anri Jul 25 '14 at 10:49
-
What is "every" index? You mean every possible index you may ever possibly want to access, which is basically infinite many indices? Or do you have only a number of defined keys? Please specify some more context. – deceze Jul 25 '14 at 11:06
-
This question was perfectly clear. Who the hell closed it? – Alasdair Aug 03 '14 at 05:02
2 Answers
1
You do not want to set a default value for every index of the array, unless your array is really quite small. It would be a waste of processing and memory.
You can do this:
if (isset($array[$index])) {
$var = $array[$index]; // the index exists
} else {
$var = 'default value'; // the index does not exist
}
// now so something with $var

Alasdair
- 13,348
- 18
- 82
- 138
0
'Object oriented' method:
You could create your own ArrayAccess
implementation, which returns a default value (null
for example) if the key you are looking for doesn't exists. The easiest way would be to extend the existing ArrayObject
class:
class MyArray extends ArrayObject {
public function offsetGet($offset) {
if(!$this->offsetExists($offset)) {
return null; // or some default value
}
return parent::offsetGet($offset);
}
}
Then use this instead of array()
:
$arr = new MyArray();
$arr['y'] = "I'm ok";
echo $arr['x']; // not set, default value (null) is returned.
echo $arr['y']; // prints "I'm ok"
Procedural method:
You could define a method, which safely gets a value from your arrays:
function safeGet($array, $key, $default = null){
if(!is_array($array) && !$array instanceof ArrayAccess){
throw new InvalidArgumentException('$array must be an array');
}
return isset($array[$key]) ? $array[$key] : $default;
}
Then simply access your values using this method:
$arr = array();
$arr['z'] = "I'm ok";
echo safeGet($arr, 'x'); // tries to access $arr['x']. prints null by default.
echo safeGet($arr, 'y', 'default'); // tries to access $arr['y']. prints 'default'.
echo safeGet($arr, 'z'); // key exists, prints "I'm ok".

Balázs Édes
- 13,452
- 6
- 54
- 89