php 将数组结果中的值存储到 codeigniter 中的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15021755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
store value from array result to variable in codeigniter
提问by Kango
I have following controller,
我有以下控制器,
class mycontroller extends CI_Controller
{
public function getvalue() {
$q= $this->db->select(array('value'))
->from('settting')
->get();
$data['data']= $q->result();
$this->load->view('myview',$data);
}
}
In my view:
在我看来:
print_r($data);
打印_r($数据);
Gives output like this
给出这样的输出
Array ( [0] => stdClass Object ( [value] => wwwwwwwwwwww ) [1] => stdClass Object ( [value] => 5454546 ) [2] => stdClass Object ( [value] => 6868 ) [3] => stdClass Object ( [value] => 9898 ) [4] => stdClass Object ( [value] => 7878 ) [5] => stdClass Object ( [value] => 212 ) [6] => stdClass Object ( [value] => 87878 ) [7] => stdClass Object ( [value] => 8989 ) [8] => stdClass Object ( [value] => 212 ) )
I am trying to store each single value in variable
我试图将每个值存储在变量中
when i use ,
当我使用时,
foreach($data as $row)
{
echo $row->value;
}
its give me all value but i need to save each value in variable.
它给了我所有的价值,但我需要将每个值保存在变量中。
how can i do this, like this,
我怎么能这样做,像这样,
$var1='wwwwwwwwwwww'; // [0 ] array value
$var2='5454546'; // [1] array value
回答by Dino
better save it to another array
最好将它保存到另一个数组
$my_values = array();
foreach($data as $row)
{
$my_values[] = $row->value;
}
now you have :
现在你有:
echo $my_values[0]; // wwwwwwwwwwww
echo $my_values[1]; // 5454546
回答by Pankaj Singh
When you print a query variable,
当您打印查询变量时,
print_r($query);
you would get following result
你会得到以下结果
Array ( [0] => stdClass Object (
[Id] => 14
[username] => john
[password] => e10adc3949ba59abbe56e057f20f883e
[email] => [email protected]
[key] => 7f55570435fd15393f27eaac659b078a
[is_active] => 1
[gender] => Man
[countryid] => 1
[stateid] => 4
[cityid] => 4
[age] => 31
) )
So if you want to get the value of a single element from this array, you can write the following code -
所以如果你想从这个数组中获取单个元素的值,你可以编写以下代码——
echo $query[0]->username;
then you get the following value
然后你得到以下值
john
I hope this solves your problems. I'm sure that you might have already found the answer. but i got the same problem, so i thought it might help somebody.
我希望这能解决您的问题。我相信你可能已经找到了答案。但我遇到了同样的问题,所以我认为它可能对某人有所帮助。
回答by Winston
You can try like this
你可以这样试试
class mycontroller extends CI_Controller
{
public function getvalue() {
$q = $this->db->select(array('value'))
->from('settting')
->get()
->result_array();
$i = 0;
foreach($q as $val)
{
${'var' . ++$i} = $val['value']; // Here we put each value in new variable, with variable name var1, var2, ...
}
echo $var1;
echo $var2;
$this->load->view('myview',$data);
}
}
回答by Muhammad Raheel
You strategy is totally wrong. You are calling a db call from a controller which breaks MVC rule. Create a model for this.
你的策略完全错误。您正在从破坏 MVC 规则的控制器调用 db 调用。为此创建一个模型。
Class Value Extends CI_Model{
function __construct(){
parent::__construct();
}
function getValue(){
return $this->db
->select(array('value'))
->from('settting')
->get();
->result();
}
And now call it from controller
现在从控制器调用它
class mycontroller extends CI_Controller
{
$this->load->model('value');
public function getvalue() {
$data['value']= $this->value->getValue();
$this->load->view('myview',$data);
}
}
And now in the view
现在在视图中
foreach($value as $row)
{
echo $row;
}
回答by Chandramohan
Look array is containing "stdClass Object" object. So you need to convert object into array. So convert using,
查找数组包含“stdClass 对象”对象。所以你需要将对象转换为数组。所以转换使用,
$data= $q->result();
$data['data']=$data->result_array(); or $data->row_array();
回答by Samy
You loop through the result with some tempoarary variable to construct variable. But i don't know whats the use of storing the array values in seperate variable :-)
您使用一些临时变量遍历结果以构造变量。但我不知道将数组值存储在单独的变量中有什么用:-)
$i = 0;
foreach($data as $row)
{
$var_.$i = $row->value;
$i++;
}

