我有一个数组作为$ result我从这个select操作得到。我正在使用codeigniter
$this->db->select('*');
$this->db->from($table);
$this->db->where('User_Id',$User_Id);
$query = $this->db->get();
$results = $query->result();
$result = $results[0];
我想将这些值保存在变量中。我正在做的是
$HeightFrom=$result->HeightFrom;
$HeightTo=$result->HeightTo;
$MaritalStatus=$result->MaritalStatus;
$Religion=$result->Religion;
$MotherTongue=$result->MotherTongue;
$Community=$result->Community;
$ProfileCreatedby=$result->ProfileCreatedby;etc....
但是我在arrary中有50个值。所以想要用于循环。我该怎么办?如何编写for循环代码。 我的问题是使用for循环我想将数组值保存为包含值
的变量名答案 0 :(得分:0)
您可以同时获取结果并将其分配给变量,如下所示:
while ($result = $query->result()) {
$HeightFrom=$result->HeightFrom;
$HeightTo=$result->HeightTo;
$MaritalStatus=$result->MaritalStatus;
$Religion=$result->Religion;
$MotherTongue=$result->MotherTongue;
$Community=$result->Community;
$ProfileCreatedby=$result->ProfileCreatedby
}
当没有更多记录可供获取时,它将停止。
注意:在当前格式中,它将在循环的每次迭代中覆盖变量 - 您必须对其执行某些操作,例如输出或将其保存在另一个数组中。
答案 1 :(得分:0)
这样做:
foreach($result as $key => $value ){
$$key = $value;
}
$$键将使用每个键的名称设置变量。例如,如果键是 HeightFrom ,则变量将是 $ HeightFrom ,因此您可以调用这些变量,所有这些变量都是50个。