in_array()

來源:互聯網
上載者:User
//現有的ID集合$set['old_id']與前台傳來的id集合$set['new_id']作對比,

public function contrast_new_old_id(){    //從前台傳來的id組    $checkids['id']=$this->input->post('id');    //根據逗號將$checkids['id']拆解,並形成新ID集合$set_new_id    $set_new_id=explode(',', $checkids['id']);    //從資料庫中取得的現有的ID的集合$set_old_id    $set_old_id=$this->m_checkout->acquire_old_id();        //print_r($set_old_id);    //將$set_new_id 中的id一個個拿出來,在$set_old_id 中作對比。將在$set_old_id 中沒有的ID存入資料庫            for ($i=0; $i < count($set_new_id); $i++) {             if(in_array($set_new_id[$i], $set_old_id)){                //當前資料庫中有的                echo "ID: ".$set_new_id[$i]." 存在
"; $this->load->view('checkout/v_test'); }else{ //將當前資料庫中沒有的寫進數組$checkout[] $checkout[$i]=$set_new_id[$i]; } } print_r($checkout); //$this->m_checkout->add_new_id($checkout);}

我傳入的值:
123,1233

print_r($set_old_id):
Array ( [0] => Array ( [ID] => 1 ) [1] => Array ( [ID] => 12 ) [2] => Array ( [ID] => 123 ) [3] => Array ( [ID] => 1234 ) [4] => Array ( [ID] => 12345 ) )

列印出來的:
ID: 123 存在
Array ( [0] => 123 [1] => 1233 )

問題:為什麼print_r($checkout);還會有123
我明明進行了篩選。
望大神告知。。謝謝

回複內容:

//現有的ID集合$set['old_id']與前台傳來的id集合$set['new_id']作對比,

public function contrast_new_old_id(){    //從前台傳來的id組    $checkids['id']=$this->input->post('id');    //根據逗號將$checkids['id']拆解,並形成新ID集合$set_new_id    $set_new_id=explode(',', $checkids['id']);    //從資料庫中取得的現有的ID的集合$set_old_id    $set_old_id=$this->m_checkout->acquire_old_id();        //print_r($set_old_id);    //將$set_new_id 中的id一個個拿出來,在$set_old_id 中作對比。將在$set_old_id 中沒有的ID存入資料庫            for ($i=0; $i < count($set_new_id); $i++) {             if(in_array($set_new_id[$i], $set_old_id)){                //當前資料庫中有的                echo "ID: ".$set_new_id[$i]." 存在
"; $this->load->view('checkout/v_test'); }else{ //將當前資料庫中沒有的寫進數組$checkout[] $checkout[$i]=$set_new_id[$i]; } } print_r($checkout); //$this->m_checkout->add_new_id($checkout);}

我傳入的值:
123,1233

print_r($set_old_id):
Array ( [0] => Array ( [ID] => 1 ) [1] => Array ( [ID] => 12 ) [2] => Array ( [ID] => 123 ) [3] => Array ( [ID] => 1234 ) [4] => Array ( [ID] => 12345 ) )

列印出來的:
ID: 123 存在
Array ( [0] => 123 [1] => 1233 )

問題:為什麼print_r($checkout);還會有123
我明明進行了篩選。
望大神告知。。謝謝

$a = [1,2,3];$b = [1,4,5];for($i = 0, $l = count($a); $i < $j; $i ++) {    if (in_array($$a[$i], $b)) {        //then...    } else {        //then...    }}

這個是正確使用in_array的例子,你那個的具體問題自己思考一下吧。看代碼也看不出$set['old_id']具體長什麼樣。

首先 $set_old_id 格式是不對的,另外在使用 in_array 函數時需要注意它的一些“怪異行為”,手冊中有很多例子展示了該函數在使用時需要注意的地方,不凡參考一下手冊 in_array。

如果 $set_old_id 格式正確的話,也就是把 ID 提取出來,形成一個一維數組,如:[1,2,3,4]。那麼在使用 in_array 函數時,這裡需要使用上第三個參數,它意味著進行嚴格匹配(類型及值都需要完全一樣)。如:

in_array($set_new_id[$i], $set_old_id, true)

這樣子才能準確判斷一個值是否存在一個數組裡,當然也可以用其他方法,比如 isset() 等等,PHP手冊有許多案例。

以前自己在使用 in_array() 過程中也中過招,記錄在這裡“PHP 中的一些 “陷阱”。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.