找到你要的答案

Q:Using variable variables to populate array results in error

Q:使用变量变量填充数组结果出错

I have the following code:

function filterUsers(array $setOfAllUsers) {
   if (empty($setOfAllUsers)) {
      return array(array(), array());
   }

   $activeUsers   = array();
   $inactiveUsers = array();
   foreach($setOfAllUsers as $userRow) {
       $var = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';

       $$var[$userRow['CID']]['Label'] = $userRow['UserLabel'];
       // Error happens here ---^

       $$var[$userRow['CID']]['UserList'][$userRow['UID']] = array(
           'FirstName' => $userRow['FName'],
           'LastName'  => $userRow['LName'],
           ... More data
       );
   }

   return array($activeUsers, $inactiveUsers);
}

I get the following error: Warning: Illegal string offset 'Label' in ...

How can I fix this? I tried defining Label part first like this: $$var[$userRow['CID']] = array(); $$var[$userRow['CID']]['Label'] = ''; but did not work.

To make things clear what I am trying to achieve is this:

if ($userRow['IsActive']) {
   $activeUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];

   $activeUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
           'FirstName' => $userRow['FName'],
           'LastName'  => $userRow['LName'],
           ... More data
   );
} else {
   $inactiveUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];

   $inactiveUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
           'FirstName' => $userRow['FName'],
           'LastName'  => $userRow['LName'],
           ... More data
   );
}

Instead of repeating above in if/else I wanted to achieve it using $$

我有以下代码:

function filterUsers(array $setOfAllUsers) {
   if (empty($setOfAllUsers)) {
      return array(array(), array());
   }

   $activeUsers   = array();
   $inactiveUsers = array();
   foreach($setOfAllUsers as $userRow) {
       $var = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';

       $$var[$userRow['CID']]['Label'] = $userRow['UserLabel'];
       // Error happens here ---^

       $$var[$userRow['CID']]['UserList'][$userRow['UID']] = array(
           'FirstName' => $userRow['FName'],
           'LastName'  => $userRow['LName'],
           ... More data
       );
   }

   return array($activeUsers, $inactiveUsers);
}

我得到以下错误:警告:非法字符串偏移'标签'…

我怎样才能解决这个问题?我试图定义标签部分首先是这样的:$变量[ userrow美元'cid ] [ ] = array();美元userrow [ 'cid VAR [美元] ] [ 'label’] = '';但没有工作。

让事情清楚我想达到的是:

if ($userRow['IsActive']) {
   $activeUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];

   $activeUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
           'FirstName' => $userRow['FName'],
           'LastName'  => $userRow['LName'],
           ... More data
   );
} else {
   $inactiveUsers[$userRow['CID']]['Label'] = $userRow['UserLabel'];

   $inactiveUsers[$userRow['CID']]['UserList'][$userRow['UID']] = array(
           'FirstName' => $userRow['FName'],
           'LastName'  => $userRow['LName'],
           ... More data
   );
}

而不是重复以上,如果/否则我想实现它使用$

answer1: 回答1:
function filterUsers(array $setOfAllUsers) {
   if (empty($setOfAllUsers)) {
      return array(array(), array());
   }

   $users = array(
     'inactiveUsers' => array(), 
     'activeUsers'   => array()
   );
   foreach($setOfAllUsers as $userRow) {
       $status = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';
       $users[$status][$userRow['CID']] = array();
       $users[$status][$userRow['CID']]['Label'] = $userRow['UserLabel'];

       $users[$status][$userRow['CID']]['UserList'] = array(
           $userRow['UID'] => array(
             'FirstName' => $userRow['FName'],
             'LastName'  => $userRow['LName'],
           )
       );
   }

   return $users;
}
function filterUsers(array $setOfAllUsers) {
   if (empty($setOfAllUsers)) {
      return array(array(), array());
   }

   $users = array(
     'inactiveUsers' => array(), 
     'activeUsers'   => array()
   );
   foreach($setOfAllUsers as $userRow) {
       $status = ($userRow['IsActive'] ? '' : 'in') . 'activeUsers';
       $users[$status][$userRow['CID']] = array();
       $users[$status][$userRow['CID']]['Label'] = $userRow['UserLabel'];

       $users[$status][$userRow['CID']]['UserList'] = array(
           $userRow['UID'] => array(
             'FirstName' => $userRow['FName'],
             'LastName'  => $userRow['LName'],
           )
       );
   }

   return $users;
}
answer2: 回答2:

Try using ${$var} instead of $$var.

EDIT

From PHP Manual (http://php.net/manual/en/language.variables.variable.php):

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

尝试使用$ { $ var }代替美元的变种。

编辑

从PHP手册(http:/ / PHP。净/手动/ EN /语言变量。变量,PHP):

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

answer3: 回答3:

Try the following:

$myUsers = $$var;
$myUsers[...][...] = ...;

The problem with using $$var[...][...] is that first $var[...][...] is evaluated and then it tries to find the variable named $$var[...][...].

尝试以下:

$myUsers = $$var;
$myUsers[...][...] = ...;

使用$ $ VAR…[…]的问题是,第一$ VAR […]…进行评估,然后它试图找到变量命名为$ VAR […] […]。

answer4: 回答4:

Don't use var-vars like this. You run into a PHP syntax glitch. And even if there wasn't a syntax glitch, you shouldn't be using var-vars in the first place. They make for near-impossible-to-debug code.

$x = 'foo';
$foo = array();
$$x[0] = 1;
var_dump($x);  // string(3) "foo"
var_dump($foo); // array(0) { }
$$x[1][2] = 3;
PHP Notice:  Uninitialized string offset: 2 in php shell code on line 1

Note how the array didn't get modified by the $$x[0] assignment, and how the 2+ dimensional assigment causes "undefined string offset". Your var-var isn't being treated as an array - it's being treated as a STRING, and failing, because it's a syntax glitch. You can treat strings as arrays, but not using var vars:

$bar = 'baz';
$bar[1] = 'q';
echo $bar; // output: bqz

There appears to be NO way to use a var-var as an array, especially a multi-dimensional array.

不要使用VAR VAR这样。你遇到一个PHP语法错误。即使没有语法错误,你不应该放在首位,利用VAR VAR。他们使几乎不可能调试代码。

$x = 'foo';
$foo = array();
$$x[0] = 1;
var_dump($x);  // string(3) "foo"
var_dump($foo); // array(0) { }
$$x[1][2] = 3;
PHP Notice:  Uninitialized string offset: 2 in php shell code on line 1

注意数组没有得到由$ X [ 0 ]分配的改进,以及如何2 +维分配导致“未定义的字符串偏移”。你的var不是作为阵列-这被视为一个字符串,和失败,因为它是一个语法错误。你可以把字符串数组,但不使用VAR VAR:

$bar = 'baz';
$bar[1] = 'q';
echo $bar; // output: bqz

似乎没有办法使用var作为数组,特别是多维数组。

php  variable-variables