Cakephp:使用表中的等效列值更新数组对象

时间:2018-12-19 00:18:32

标签: php mysql arrays cakephp-1.3

我已将以下内容存储在设置表中:

Array(
    A => 2, B => 2, C => 0, D => 2,
    E => 2, F => 0, G => 2, H => 0,
    I => 2, J => 0, K => 0, L => 0,
    M => 0, N => 0, O => 0, P => 0,
    Q => 0
)

,并希望在代码中使用时将别名列的值替换为id的列的值,以便得到:

Array(
    35 => 2, 24 => 2, 14 => 0, 12 => 2,
    22 => 2, 8 => 0, 21 => 2, 17 => 0,
    7 => 2, 10 => 0, 9 => 0, 13 => 0,
    18 => 0, 15 => 0, 16 => 0, 6 => 0, 11 => 0
)

表格数据:
enter image description here

在Cakephp中寻找一种更简单的方法来实现这一目标。谢谢。

1 个答案:

答案 0 :(得分:0)

因此,如果您将从数据库中检索到的数据存储在这样的数组中:

hive> CREATE EXTERNAL TABLE table1( id STRING, name STRING, `12489738` STRING, 
     `12492628` STRING, 
     `12492633` STRING, `12492638` STRING, `12492655` STRING, `12492659` STRING)
      ROW FORMAT DELIMITED FIELDS TERMINATED BY "\t" LOCATION "" 
      tblproperties ("skip.header.line.count"="1");

现在,我们可以比较数组并进行如下替换:

$arr1 = Array( 35 => 'A', 24 => 'B', 14 => 'C', 12 => 'D', 22 => 'E', 8 => 'F')
$arr2 = Array( 'A' => 2, 'B' => 2, 'C' => 0, 'D' => '2', 'E' => 2, 'F' => 0)

查看实际情况here