PHP - 如何设置数组的每个索引的默认值

时间:2021-11-08 17:09:19

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?

我在处理数组时遇到问题。如果我访问一个不存在的数组的索引,PHP会抛出一个错误,并且每个索引的调整都很困难。有没有什么办法可以为数组的每个索引设置默认值,所以即使我访问一个不存在的数组索引然后它返回默认的设置值?

2 个解决方案

#1


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

#2


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:

您可以创建自己的ArrayAccess实现,如果您要查找的键不存在,则返回默认值(例如null)。最简单的方法是扩展现有的ArrayObject类:

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() :

然后使用它而不是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".

#1


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

#2


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:

您可以创建自己的ArrayAccess实现,如果您要查找的键不存在,则返回默认值(例如null)。最简单的方法是扩展现有的ArrayObject类:

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() :

然后使用它而不是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".