簡體   English   中英

如何在PHP Codeigniter中合並兩個不同數組的兩個不同值

[英]How to Merge two different values of two different array in php codeigniter

我有這種桌子

a

我想這樣獲取數據

一種

我已經獲取了菜名(文本),但我還需要在單個數組中使用它的ID,我該怎么做? 我的代碼如下:控制器:-

function fetch()
    {
        $this->load->model('fetch');
        $d=$this->fetch->call();
        $e=$d->result();
        $max=$d->num_rows();
        for($i=0;$i<$max;$i++){
            $a[$i]=$e[$i]->product;
            $a2[$i]=$e[$i]->id;
            $b[]=explode(',',$a[$i]);
        }
        echo '<pre>';
        print_r($a2);
        $array['arr'] = call_user_func_array('array_merge', array_map('array_values', $b));
        $this->load->view('display',$array);
    }

模型:-

function call()
    {
        $this->db->select('*');
        $this->db->from('ruff');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            return false;
        }
    }

視圖:-

<body>
<input type="text" name="item" id="txt_search" list="item_list">
<datalist id="item_list">
    <?php
        foreach($arr as $a)
        {
            echo "<option>".$a."</option>";
        }
    ?>
</datalist>
</body>

提前致謝。

在您的控制器中嘗試此...代碼

 $this->load->model('fetch');
    $d=$this->fetch->call();
    $e=$d->result_array();
    // $max=$d->num_rows();
    $products='';
    foreach ($e as $p) {
      $products.=$p['product'];
    }

    $data=explode(',', $products);

    echo '<pre>';
    print_r($data);
    echo '</pre>';

通常,在控制器中訪問數據庫功能不是一個好習慣。 因此,為了滿足計算項目的需求,您可以更改模型以返回項目數組。

模型:

function call()
{
    $data = array();

    $this->db->select('*');
    $this->db->from('ruff');
    $query = $this->db->get();

    if ($query->num_rows() > 0) {
        foreach($query->result() as $row){
            $data[] = (array)$row;
        }
        return $data;
    } else {
        return false;
    }
}

然后,在控制器的fetch功能中,您可以展開product字段並創建另一個數組以提交到視圖。

控制器:

function fetch()
{
    $this->load->model('fetch');
    $d=$this->fetch->call();

    $list = array();

    foreach($d as $row)
    {
        $products = explode(',', $row['product']);
        foreach($products as $p)
        {
            $list[] = array('id' => $row['id'], 'product' => $p);
        }
    }

    $this->load->view('display',array('arr' => $list));
}

這將導致您擁有一個如下數組:

Array
(
[0] => Array
    (
        [id] => 1
        [product] => veg. burger
    )

[1] => Array
    (
        [id] => 1
        [product] =>  non-veg burger
    )

[2] => Array
    (
        [id] => 1
        [product] =>  chees burger
    )

[3] => Array
    (
        [id] => 2
        [product] => vadapau
    )

[4] => Array
    (
        [id] => 2
        [product] =>  butter vadapau
    )

[5] => Array
    (
        [id] => 2
        [product] =>  chees vadapau
    )
)

現在,您的ID在您的視圖中可用。

視圖:

<body>
    <input type="text" name="item" id="txt_search" list="item_list">
    <datalist id="item_list">
    <?php
        foreach($arr as $a)
        {
            echo "<option>".$a['product']."</option>";
            //$a['id']
        }
    ?>
    </datalist>
</body>

這是您的模型代碼。

public function call()
{
   $query = $this->db->select("id,product")
                        ->from("stuff")
                        ->get();
    if ($query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return false;
    }
}

現在,您將在控制器中獲得數據的結果數組。 這是控制器功能。

public function fetch()
{
    $this->load->model('fetch');

    $result = $this->fetch->call();
    foreach ($result as $value) {
        $newArr = explode(',',$value['product']);   
        foreach ($newArr as  $val) {
            $productArray[]  = array('productID'=>$value['id'],"product"=>$val);
        }
    }
    $data['finalArray'] = $productArray;
    $this->load->view('display',$data);

}

現在訪問視圖中的最終產品陣列。 這是查看代碼。

<input type="text" name="item" id="txt_search" list="item_list">
<table id="item_list" border="1">
    <?php

        foreach($finalArray as $stuffs)
        {
            echo "<tr>";
            echo "<td>".$stuffs['productID']."</td>";
            echo "<td>".$stuffs['product']."</td>";
            echo "</tr>";
        }
    ?>
</table>

干杯!

它的代碼非常簡單,請在下面的代碼中編寫您的模型代碼:

模型:-

function call() { return $this->db->get('ruff')->result_array(); }

此代碼現在為您提供所有數據,如下所示的控制器代碼

控制器: -

function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }

此代碼現在為您提供所有數據,如下所示的控制器代碼

控制器: -

function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); } function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }此代碼現在給你的所有數據控制器代碼如下圖所示

控制器: -

function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }

現在,請在視圖View中編寫代碼:

`此代碼現在為您提供所有數據,如下所示,控制器代碼

控制器:- function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); } function fetch() { $this->load->model('fetch'); $array['arr'] = $this->fetch->call(); $this->load->view('display',$array); }

現在,請在視圖View中編寫代碼:

<body> <input type="text" name="item" id="txt_search" list="item_list"> <datalist id="item_list"> <?php foreach($arr as $value){ $newarray=explode(',',$value); foreach($newarray as $test) { echo "<option value=".$arr['id'].">".$test['product']."</option>"; } } ?> </datalist> </body>

我認為這段代碼將對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM