我在使用CI和Datatables时遇到了问题 ,有人可以帮助我吗
源代码
模态代码
public function all() {
$query = $this->db->get($this->table);
return $query->result_array();
}
SQL返回
数组([0] =&gt;数组([id_barang] =&gt; 1 [nama_barang] =&gt;希捷硬盘外部500GB [stok_barang] =&gt; 100 [harga_awal_barang] =&gt; 400000 [harga_jual_barang] =&gt; 600000)[1] =&gt;数组([id_barang] =&gt; 2 [nama_barang] =&gt; USB 8gb Advan [stok_barang] =&gt; 50 [harga_awal_barang] =&gt; 50000 [harga_jual_barang] =&gt; 80000))< / p>
控制器代码
public function test(){
$this->load->model('gudang_model');
$data=$this->gudang_model->all();
$output=array();
foreach ($data as $row) {
//$column = array();
for ($i=0; $i<count($data); $i++) {
$column[] = $row;
}
$output['data'] = $column;
}
echo json_encode($output);
}
结果
{&#34;数据&#34;:[{&#34; id_barang&#34;:&#34; 1&#34;,&#34; nama_barang&#34;:&#34;希捷硬盘外置500GB& #34;&#34; stok_barang&#34;:&#34; 100&#34;&#34; harga_awal_barang&#34;:&#34; 400000&#34;&#34; harga_jual_barang&#34;:& #34; 600000&#34;},{&#34; id_barang&#34;:&#34; 1&#34;,&#34; nama_barang&#34;:&#34;希捷硬盘外置500GB&#34;,& #34; stok_barang&#34;:&#34; 100&#34;&#34; harga_awal_barang&#34;:&#34; 400000&#34;&#34; harga_jual_barang&#34;:&#34; 600000&# 34;},{&#34; id_barang&#34;:&#34; 2&#34;,&#34; nama_barang&#34;:&#34; USB 8gb Advan&#34;,&#34; stok_barang&#34 ;:&#34; 50&#34;&#34; harga_awal_barang&#34;:&#34; 50000&#34;&#34; harga_jual_barang&#34;:&#34; 80000&#34;},{& #34; id_barang&#34;:&#34; 2&#34;,&#34; nama_barang&#34;:&#34; USB 8gb Advan&#34;,&#34; stok_barang&#34;:&#34; 50&#34;&#34; harga_awal_barang&#34;:&#34; 50000&#34;&#34; harga_jual_barang&#34;:&#34; 80000&#34;}]}
有人可以帮助我知道控制器中的循环如何得到像这样的结果
{&#34;数据&#34;:[[&#34; 1&#34;,&#34;希捷硬盘外置500GB&#34;&#34; 100&#34;&#34; 400000& #34;],[&#34; 2&#34;,&#34; USB 8gb Advan&#34;,&#34; 50&#34;,&#34; 50000&#34;,&#34; 80000&# 34;]]}
谢谢
答案 0 :(得分:1)
在这种情况下你可以使用array_values()
:
$data = $this->gudang_model->all();
$output = array();
foreach($data as $k => $value) {
$output[] = array_values($value);
}
echo json_encode(array('data' => $output));
应该看起来像this