2016-08-29 5 views
2

コントローラーから2つの配列を結合するにはどうしたらいいですか?私はAND演算子を以下のように使用しようとしましたが失敗しました。どうすればそれを可能にすることができますか?ここコントローラーから2つの配列を結合するにはどうすればいいですか

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>';   
?> 

マイモデル

私は、コントローラからの二つの配列を結合するにはどうすればよい
function get_products($category_id) 
{ 
    $this->db->select('*'); 
    $this->db->from('categories c'); 
    $this->db->join('products p', 'c.category_id=p.category_id', 'left'); 
    $this->db->where('c.category_status = 0'); 
    $this->db->where('c.category_id', $category_id); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

function get_price($category_id) 
{ 
    $this->db->select('*'); 
    $this->db->from('categories c'); 
    $this->db->join('price r', 'c.category_id=r.category_id', 'c.product_id=r.product_id', 'left'); 
    $this->db->where('c.category_status = 0'); 
    $this->db->where('c.category_id', $category_id); 
    $query = $this->db->get(); 
    return $query->result_array(); 
} 

+0

両方のアレイのデータ構造を投稿してください – Kisaragi

+0

なぜ必要なのですか?私はあなたが '$ price'にアクセスしていることに気が付いていますが、コントローラ内で変数 'price'を' $ this-> data ["prices"] = $ this-> select-> get_price($ category_id ); ' – DFriend

答えて

0

array_map

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); 

ソースを使用、2以上の配列を反復する:http://php.net/manual/en/function.array-map.php

0

を言及したコードの下あたりのように、あなたのビューを変更してください。

<?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>';   
?> 
関連する問題