代码之家  ›  专栏  ›  技术社区  ›  morne

array_set-允许fullstops的escape变量

  •  0
  • morne  · 技术社区  · 8 年前

    array_set 创建一个数组,该数组的值用于报表。

    我的问题是 $key

    价值 {$this->totalmonths} OUTCOME

    e、 g.Im加载项 一个值 remained_on_unit
    [0]['OUTCOME_remained_on_unit'] = 1

    但有时有一些值有句号。
    $键 等于 moved_to_I.C.U ,从而导致
    [0]['OUTCOME_I'][C][U] = 1

    我怎么逃走 $键

    array_set($months, "{$this->totalmonths}.$key", $totalcount+1)

    2 回复  |  直到 8 年前
        1
  •  2
  •   Richard Parnaby-King    8 年前

    这是预期的功能 array_set

    array_set函数使用“点”符号在深度嵌套数组中设置值:

    为了达到你所追求的目标:

    //$this->totalmonths = 'OUTCOME'
    //$key = moved_to_I.C.U
    //$this->totalmonths . $key = 'OUTCOMEmoved_to_I.C.U'
    $months[$this->totalmonths . $key] = $totalcount+1;
    
        2
  •  0
  •   Rafael    8 年前

    $key 变量。如果 点 包含在其中,您可以像已经在做的那样连接并放大 array_set() 剩下的应该做。

    如果只想设置任何嵌套元素的键,则需要完整路径,直到元素与点连接为 .

    所以如果你想要 ['OUTCOME_I'][C][U] 要成为1,您需要将其设置为:

    $this->totalMonths = 'OUTCOME';
    $key = '_I.C.U';
    
    array_set($array, "{$this->totalmonths}.$key", 1);
    

    $this->totalMonths = 'OUTCOME';
    $key = 'moved_to_I.C.U';
    
    echo "{$this->totalmonths}.$key";
    

    会输出这个吗?

    OUTCOME_I.C.U
    
    推荐文章