1

我有一段代码,我从 SQL 中获取多行数据,并且在其中一列中有一个数值,如果它等于某个数字,我需要检查并将数据更改为文本。为了做到这一点,我需要知道数组元素是如何命名$results[????]的,以便获取并更改它。那么在使用 SQL/Cake 时,数组的命名约定是什么?

这是结块的代码:

$params = array(
    'fields' => array(
        $this->name . '.AUTHORIZE_PROVIDER_NAME',           
        $this->name . '.SOURCE_ID',
        $this->name . '.ORDER_ITEM_TITLE',
        $this->name . '.DOSE_AMOUNT',      
        $this->name . '.DOSE_UNIT',
        $this->name . '.DT_CREATED_TIME',
        $this->name . '.ROUTE_ID',
        $this->name . '.SEQUENCE_NO',
        $this->name . '.LOCATION',
        $this->name . '.BODY_SITE_ID',
        $this->name . '.COMMENT', 
        'DD.DICTIONARY_DATA_CODE',
    ),

    /*
    'conditions' => array(
        //conditions
        $this->name . '.HID'    => $hospital_id,
        $this->name . '.PID'    => $patient_id,                
    ),
    */

    'order' => array(
        $this->name . '.DT_CREATED_TIME',
    ),
    'joins' => array(
        array(
            'table'     => 'DICTIONARY_DATA',
            'alias'     => 'DD',
            'type'      => 'INNER',
            'fields'    => 'DD.DICTIONARY_DATA_CODE as DD_Code',
            'conditions'=> array(
                $this->name . '.PRIORITY_ID = DD.DICTIONARY_DATA_ID',
                $this->name . '.HID' => $hospital_id,
                $this->name . '.PID' => $patient_id,
            )
        )
    ),
);
$rs = $this->find('all', $params);

我在这里获取数据:

foreach ($rs as $record){
    try {
        $result[] = $record[$this->name];
        array_push($result, $record['DD']);
    }
}

并返回它以作为 JSON 对象打印出来。所以我想检查一下and$results[]的数值。我怎么能做到这一点而不做?SOURCE_IDROUTE_IDforeach

4

1 回答 1

0

我想到了:

使用结块 SQL 语句时,会返回一个 3-D 数组(当只请求一个字段或选择时,会返回 2-D)。它们被命名为:

Array(
    Array[table_name] =>
        [column_name] => field value
        [column_name] => field value
        .
        .
    Array[table_name] =>
        [column_name] => field value
        [column_name] => field value
        .
        .
    .
    .
);

并且当每个都通过 foreach 语句运行时,元素会根据它在数组中的位置更改为数字[table_name][column_name]现在[0],或等。[1]

为了检查ROUTE_IDSOURCE_ID我创建了一个哈希表这样的数值

$sourceValues = array(
        500002 => 'Verbal',
        500003 => 'Telephone',
        500004 => 'Written',
        500005 => 'Other'
    );

$routeValues = array(
        11     => 'Intramuscular',
        22     => 'Nasal',
        28     => 'Subcutaneous'
    );

并遍历 and 的每一行的值,SOURCE_ID如下ROUTE_ID所示:

foreach($record as $value){
           $source = $value['SOURCE_ID'];
           $route = $value['ROUTE_ID'];
           $value['SOURCE_ID'] = $sourceValues[$source];
           $value['ROUTE_ID'] = $routeValues[$route];
           $result[] = $value;
    }
于 2012-07-19T16:28:11.590 回答