将vars分配到数组时PHP的奇怪情况

时间:2015-01-17 14:20:32

标签: php arrays

我正在构建一个SQL查询。奇怪的是,根据阵列内部的位置,正确或错误地分配了一些变量。这是代码:

$v = [...] // $v is an array containing a serialized form
$id = 20;
foreach ($v as $key => $value) {
    $key = explode('c_', $key);
    $key = $key[1]; // this is the id of the fieldset that is being processed
    // array containing the data to be inserted with the query
    $data = array(
        'a'         => (empty($v['a_'.$key]) ? NULL : $v['a_'.$key]), 
        'b'         => (empty($v['b_'.$key]) ? NULL : $v['b_'.$key]), 
        'c'         => $value,
        'd'         => (empty($v['d_'.$key]) ? NULL : $v['d_'.$key]), 
        'e'         => (empty($v['e_'.$key]) ? NULL : $v['e_'.$key]),
        'id'        => $id
    );
    // array containing the format of the data of the array $data
    $format = array(
        (empty($v['a'.$key]) ? NULL : '%s'),
        (empty($v['b_'.$key]) ? NULL : '%s'),
        '%s',
        (empty($v['d_'.$key]) ? NULL : '%s'),
        (empty($v['e_'.$key]) ? NULL : '%s'),
        '%d'
    );
    [...] // then i send the query to the db
}

这里发生的是:

  • 如果要分配的所有数据都从false检查返回empty()(因此它们不是NULL),一切正常;

  • 如果我省略'c' => $value之前的数据,一切正常。例如:

    $data = array(
        'c'         => $value,
        'd'         => (empty($v['d_'.$key]) ? NULL : $v['d_'.$key]), 
        'e'         => (empty($v['e_'.$key]) ? NULL : $v['e_'.$key]),
        'id'        => $id
    );
    
  • 如果我在所有其他变量之前分配'c' => $value'id' => $id,一切正常。例如:

    $data = array(
        'c'         => $value,
        'id'        => $id,
        'a'         => (empty($v['a_'.$key]) ? NULL : $v['a_'.$key]), 
        'b'         => (empty($v['b_'.$key]) ? NULL : $v['b_'.$key]), 
        'd'         => (empty($v['d_'.$key]) ? NULL : $v['d_'.$key]), 
        'e'         => (empty($v['e_'.$key]) ? NULL : $v['e_'.$key])
    );
    
  • 在所有其他情况下,使用此问题开头显示的代码,cid将设置为''

    < / LI>

当然,我试图同时回复$value$id:两者都被正确分配。这真是太奇怪了!

更新

根据Kim的要求,这里是var_dump($v)的一个例子:

array (size=[...])
  [...]
  'a_0' => string 'test' (length=4)
  'b_0' => string 'test' (length=4)
  'c_0' => string 'test' (length=4)
  'd_0' => string 'test' (length=4)
  'e_0' => string 'test' (length=4)
  'a_1' => string 'test' (length=4)
  'b_1' => string 'test' (length=4)
  'c_1' => string 'test' (length=4)
  'd_1' => string 'test' (length=4)
  'e_1' => string 'test' (length=4)
  [...]

此外,我还尝试直接分配值'c' => 'This is a test',并始终将''返回到最终数组中。根据Jason的怀疑,我认为这证明了问题出现在发布的代码中。

1 个答案:

答案 0 :(得分:-4)

首先,在循环中和循环期间重新分配var是不好的做法

所以你的代码:

foreach ($v as $key => $value) {
    $key = explode('c_', $key);
    $key = $key[1]; // this is the id of the fieldset that is being processed

必须替换为:

foreach ($v as $index => $value) {
    $keyArr = explode('_', $index);
    $key = $keyArr[1]; // this is the id of the fieldset that is being processed

这将使您的代码更具可读性而不会产生混淆。

顺便问一下,你确定你真的需要foreach ($v[]吗?

对我而言,似乎最好改为:

$executedKey=null;
foreach ($v as $index => $value) {
        $keyArr = explode('_', $index);
        $key = $keyArr[1];
        if ($key == $executedKey) {
          continue;
        } else {
           $executedKey = $key;
        };

因此,对于您的$v示例,它只会循环2次,而不是10次。