php in_array() 期望参数 2 是数组,给定 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21333469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:03:29  来源:igfitidea点击:

in_array() expects parameter 2 to be array, null given

phparrayssymfonyfosuserbundlepugxmultiuserbundle

提问by Gianni Alessandro

I made a web-application with Symfony2, and I'm using PUGX User Bundle and FosUserBundle to manage 2 different users.

我用 Symfony2 制作了一个网络应用程序,我使用 PUGX User Bundle 和 FosUserBundle 来管理 2 个不同的用户。

This is one of the two user:

这是两个用户之一:

  /**
   * @ORM\Entity
   * @ORM\Table(name="user_Operator")
   * @ORM\HasLifecycleCallbacks()
   * @UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User", message="Username already_used")
   * @UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User", message="Email already_used")
   */
  class UserOperator extends User
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;

          /**
       * @ORM\PrePersist
       */
      public function setCreatedAtValue()
      {
          $this->addRole('ROLE_OPERATOR');
      }    
  }

When I try to register, I fill the form, submit, than appears:

当我尝试注册时,我填写表格,提交,然后出现:

Warning: in_array() expects parameter 2 to be array, null given in C:\BitNami\wampstack-
5.4.23-0\frameworks\symfony\vendor\friendsofsymfony\user-    
bundle\FOS\UserBundle\Model\User.php line 142

The line 142 is the following:

第 142 行如下:

   135    public function addRole($role)
          {
              $role = strtoupper($role);
              if ($role === static::ROLE_DEFAULT) {
                  return $this;
              }

   142        if (!in_array($role, $this->roles, true)) {
                  $this->roles[] = $role;
              }

              return $this;
          }

Don't know because I have this problem since I made an association @ORM\ManyToMany between User and Mission, that is another entity that here doesn't appear. Before this I hadn't this problem.

不知道是因为我有这个问题,因为我在 User 和 Mission 之间建立了 @ORM\ManyToMany 关联,这是这里没有出现的另一个实体。在此之前我没有这个问题。

I use PUGXUser Bundle because it helps to easily manage two different kind of user. The User Entity is in my bundle, extends FosUserBundle..../Model/User.php and is extended by UserOperator and UserGroundStation.

我使用 PUGXUser Bundle 是因为它有助于轻松管理两种不同类型的用户。用户实体在我的包中,扩展了 FosUserBundle..../Model/User.php 并由 UserOperator 和 UserGroundStation 扩展。

The definition of the role is in the FosUserBundle.../Model/User.php:

角色的定义在 FosUserBundle.../Model/User.php 中:

/**
 * @var array
 */
protected $roles;

and the costruct is:

和 costruct 是:

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

回答by Victor Bocharsky

Try to use:

尝试使用:

if (is_array($this->roles)) {
    if (!in_array($role, $this->roles, true)) {
        $this->roles[] = $role;
    }
}

回答by FuturWebs

in_array() expects parameter 2 to be array, null given

in_array() expects parameter 2 to be array, null given

For me, i solved this problem just with parent::__construct(); on my entity

对我来说,我解决了这个问题parent::__construct();在我的实体上

public function __construct()
{
    parent::__construct();
}

回答by Netzelch

You can code it in one line... nessesary if an array not always exists

您可以在一行中对其进行编码......如果数组不总是存在的话

if ( is_array($maybe_array) AND in_array($needle, $maybe_array) ) {...

because of the handling of the AND-clause your in_array will not be proved if is_array is false.

由于处理 AND 子句,如果 is_array 为假,则不会证明 in_array 。

回答by B.K

It can also help to first check if the array is not empty. For example:

它还可以帮助首先检查数组是否为空。例如:

if (!empty($this->roles) && !in_array($role, $this->roles, true)) 

回答by Mohd Abdul Baquee

/*Item's lists */
    $items = array('Apple', 'Banana', 'Cherry', 'Coconut', 'Guava', 'Lemon', 'Mango');
/*Fruit's lists to be search in Items */
    $fruits = array('Apple', 'Coconut', 'Mango');
    foreach ($items as $item) {
        if (in_array($item, $fruits)) {
    $checked = "checked";
        } 
        else{
    $checked = "unchecked";
        }
   echo '< input type="checkbox" name="checkboxes[]"' . $checked . '  />';
   }