如何在php中为数组定义默认值以避免未定义的偏移量?

如何在php中为数组定义默认值以避免未定义的偏移量?

问题描述:

I am using this code using isset to check if empty:

    if(!isset($this->objCodes[0]->SignupCode))$this->objCodes[0]->"NULL";

But it doesn't work. Help?

我使用此代码使用isset检查是否为空: p>

  if(!isset($ this-> objCodes [0]  - > SignupCode))$ this-> objCodes [0]  - >“NULL”; 
  code>  pre> 
  
 

但它不起作用。 帮助? p> div>

You might want to use empty to check if the value has a value that might be useless. If your SignupCode will never be an empty string (""), 0, or another empty value, try this:

if(empty($this->objCodes[0]->SignupCode))
{
    $this->objCodex[0] = null;
}

The reason I normally use empty() is because if I am importing from a database, the data I am looking for will always return true to isset() because it was set by the database. I'm more interested if it is null or an empty string, as that means there really isn't anything of value in the variable.

If you are loading values into your object like this:

$result = mysql_query("Some Query");
while($temp = mysql_fetch_assoc($result)
{
    $myObj = new stdClass;
    $myObj->SignupCode = $temp['SignupCode'];
    $myObjs[] = $myObj;
}

You will always recieve a true to the isset() function when testing like this:

if(isset($myObjs[0]->SignupCode))

Because the value is technically always going to be 'set'

If you are actually trying to see if it isn't set at all, try this:

if(!isset($this->objCodes[0]->SignupCode))
{
    $this->objCodex[0] = null;
}

Well is $this->objCodes defined? If so is it an array? It would also work as a string since you can treat a sring ans an array of single chars. Also with your statement youre actually checking if SignupCode isset on an object... not if $this->objCodes[0] is set.

I believe the error you're receiving is from the invalid property accessor ->"NULL".

The isset function should be used to check if an array index exists, such as the [0] index you check on objCodes:

if(isset($this->objCodes[0])) {

Without knowing the type of object you currently have in the objCodes array that has the property of SignupCode I can't be specific in an answer for setting a default but if I use the class ObjectCode as an example you could set a default value on your array element like this:

if(!isset($this->objCodes[0])) {
  $this->objCodes[0] = new ObjectCode();
}

If I knew more about what you're trying to achieve with that code block I could be more descriptive.

UPDATE: If you really wanted to ensure that your collection always gave back a value you could create a container that implemented the ArrayAccess interface and then implement the offsetGet method to return a default when the requested index is not found:

public class ObjectCodeContainer implements ArrayAccess {
  private $collection;

  public function __construct() {
    $this->collection = array();
  }

  public function offsetGet($offset) {
    if(!isset($this->collection[$offset])) {
      $this->collection[$offset] = new ObjectCode();
    }

    return $this->collection[$offset];
  }

  // implement the other abstract methods
}

$default = array('color' => 'blue', 'model' => 'Verizon', 'sex' => 'Female');

$new = array('color' => 'red', 'sex' => 'Male');

$array = array_merge($default, $new);

var_dump($array);
// array('color' => 'red', 'model' => 'Verizon', 'sex' => 'Male')