数组中的不同位置

时间:2012-03-30 11:47:52

标签: php codeigniter

我有一个sql查询,它输出一个数组,输出看起来像这个

Array
(
[0] => Array
    (
        [customer_id] => 7
        [language_id] => 1
        [variableitem_id] => 13
        [name] => QUESTION_HEADLINE
        [value] => Bitte geben Sie Ihren Downloadkey ein:
    )

[1] => Array
    (
        [customer_id] => 7
        [language_id] => 1
        [variableitem_id] => 15
        [name] => QUESTION_BUTTON
        [value] => Start!
    )

[2] => Array
    (
        [customer_id] => 7
        [language_id] => 1
        [variableitem_id] => 6
        [name] => PAGETITLE
        [value] => Steigenberger Hotels and Resorts - Mediathek
    )
)

在我的控制器中,我将其视为

$data['variables_data'] = $this->Home_model->getVariables($customer_id, $language_id);

现在,对于

中的网址中的不同ID
 localhost/home/user/12 and  localhost/home/user/14

变量的位置不同

例如在我的视图中回显

$variable[0]['value']

它为一个用户提供QUESTION_HEADLINE,为另一个用户提供PAGE_TITLE。 是否有可能让所有用户都能使用它们,就像我回应

一样
 $variable[0]['value'] 

它应该每次都为每个用户返回QUESTION_HEADLINE

Home model get_variables函数的代码

function getVariables($customer_id, $language_id) {
    $query = $this->db->query("SELECT customers_idcustomers AS customer_id, 
                               languages_idlanguages AS language_id, 
                               variableitems_idvariableitems AS variableitem_id,
                               variableitem AS name, 
                               variabletext AS value
                               FROM variables v
                               LEFT JOIN variableitems vi ON v.variableitems_idvariableitems = vi.idvariableitems
                               WHERE v.customers_idcustomers ='" . $customer_id . "' 
                               AND v.languages_idlanguages =" . $language_id
    );
    $var = $query->result_array();
    return $var;
}

提前致谢

2 个答案:

答案 0 :(得分:0)

您可以明确设置,例如

foreach($outArray as $output) { $output['name']="Any thing you want"; }

答案 1 :(得分:0)

因此,据我所知,您希望将输出减少到一个维度。它也可以用MYSQL完成。这里有php版本:

在您的控制器中

$data = $this->Home_model->getVariables($customer_id, $language_id);
$data['variables'] = array(
    'customer_id' => $data[0]['customer_id'],
    'language_id' => $data[0]['language_id'],
    'variableitem_id' => array(),
    'name' => array(),
    'value' => array()
);

foreach($data as $k => $v) {
   $data['variables']['variableitem_id'][] = $v['variableitem_id'];
   $data['variables']['name'][] = $v['name'];
   $data['variables']['value'][] = $v['value'];
}

在你看来

 echo $variables['value'][2].' '.$variables['value'][3];
 //do whatever you want