In_array ()

Source: Internet
Author: User
The existing ID collection $set[' old_id ') contrasts with the ID set $set[' new_id ' from the foreground,

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

The value I passed in:
123,1233

Print_r ($set _old_id):
Array ([0] = = Array ([id] = 1) [1] = = Array ([id] = +) [2] = = Array ([ID] + 123) [3] = Ar Ray ([id] = 1234) [4] = = Array ([id] = 12345))

Print out of:
Id:123 exists
Array ([0] = 123 [1] = 1233)

Question: Why Print_r ($checkout); there will be 123.
I was clearly screened.
Hope the great God told. Thank you

Reply content:

The existing ID collection $set[' old_id ') contrasts with the ID set $set[' new_id ' from the foreground,

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

The value I passed in:
123,1233

Print_r ($set _old_id):
Array ([0] = = Array ([id] = 1) [1] = = Array ([id] = +) [2] = = Array ([ID] + 123) [3] = Ar Ray ([id] = 1234) [4] = = Array ([id] = 12345))

Print out of:
Id:123 exists
Array ([0] = 123 [1] = 1233)

Question: Why Print_r ($checkout); there will be 123.
I was clearly screened.
Hope the great God told. Thank you

$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...    }}

This is the correct use of the In_array example, your specific question to think about it. Look at the code also do not see $set[' old_id ', what is the specific length of what.

First, the $set_old_id format is not correct, in addition to the use in_array of functions need to pay attention to some of its "strange Behavior", the manual has many examples of the function in the use of the need to pay attention to the place, uncommon Reference manual In_array.

If $set_old_id the format is correct, that is, the ID is extracted to form a one-dimensional array, such as: [1,2,3,4]. So when using the In_array function, the third parameter is used here, which means strict matching (both types and values need to be exactly the same). Such as:

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

This way to accurately determine whether a value exists in an array, of course, you can use other methods, such as isset() , and so on, PHP manual has many cases.

I used to be in the in_array() process of using the same, recorded here, "some of PHP traps."

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.