Php 如何从控制器连接两个阵列
如何从控制器连接两个阵列?我尝试使用Php 如何从控制器连接两个阵列,php,arrays,codeigniter,controller,Php,Arrays,Codeigniter,Controller,如何从控制器连接两个阵列?我尝试使用和操作符,如下所示,但失败了。我怎样才能做到这一点 function view_product($category_id) { $category_id=$this->uri->segment(3); $this->load->model('select'); $this->data["products"]=$this->select->get_products($category_id);
和操作符,如下所示,但失败了。我怎样才能做到这一点
function view_product($category_id)
{
$category_id=$this->uri->segment(3);
$this->load->model('select');
$this->data["products"]=$this->select->get_products($category_id);
$this->data["prices"]=$this->select->get_price($category_id);
$this->load->view("header");
$this->load->view("products",
$this->data);
}
以下是我的看法:
<?php
$n=0;
foreach(($products as $product) && ($prices as $price) ) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$product['product_id'].'</a></td>
<td>'.$product['product_name'].'</td><td>'.$product['product_description'].'</td><td>'.$price['price_per_unit'].'</td><td>'.$price['price_per_item'].'</td></tr>';
}
echo '</table>';
?>
如何从控制器连接两个数组?要迭代两个数组,请使用数组\u映射
function output_prices($product,$price) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$product['product_id'].'</a></td>
<td>'.$product['product_name'].'</td><td>'.$product['product_description'].'</td><td>'.$price['price_per_unit'].'</td><td>'.$price['price_per_item'].'</td></tr>';
}
array_map('output_prices',$products,$prices);
功能输出价格($product,$price){
$n++;
回显'.$n.'.$product['product_id'].'
“.$product['product_name'..”.$product['product_description'..”.$price['price_per_unit'..”.$price['price_per_item'.];
}
数组映射(输出价格,$products,$prices);
来源:请按照下面提到的代码更改您的视图
<?php
$n=0;
$merg_detls=array_merge($products,$prices);
foreach($merg_detls as $detl) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$detl['product_id'].'</a></td>
<td>'.$detl['product_name'].'</td><td>'.$detl['product_description'].'</td><td>'.$detl['price_per_unit'].'</td><td>'.$detl['price_per_item'].'</td></tr>';
}
echo '</table>';
?>
请发布两个阵列的数据结构为什么需要?我注意到,在视图中,您正在访问$price
,但在控制器中,您正在使用行$this->data[“prices”]=$this->select->get\u price($category\u id)命名变量'price'代码>
<?php
$n=0;
$merg_detls=array_merge($products,$prices);
foreach($merg_detls as $detl) {
$n++;
echo '<tr><td>' .$n .'</td><td>'.$detl['product_id'].'</a></td>
<td>'.$detl['product_name'].'</td><td>'.$detl['product_description'].'</td><td>'.$detl['price_per_unit'].'</td><td>'.$detl['price_per_item'].'</td></tr>';
}
echo '</table>';
?>