in_array()

WBOY
Release: 2016-08-10 09:07:26
Original
1190 people have browsed it

//Compare the existing ID set $set['old_id'] with the ID set $set['new_id'] sent from the front desk,

<code>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]." 存在<br>";

                $this->load->view('checkout/v_test');

            }else{
                //将当前数据库中没有的写进数组$checkout[]
                $checkout[$i]=$set_new_id[$i];    

            }
        }
    print_r($checkout);
    
    //$this->m_checkout->add_new_id($checkout);
}</code>
Copy after login
Copy after login

The values ​​I passed in:
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 ) )

Printed:
ID: 123 exists
Array ( [0] => 123 [1] => 1233 )

Question: Why does print_r($checkout); still have 123
I have obviously screened it.
Hope God will inform you. . Thank you

Reply content:

//Compare the existing ID set $set['old_id'] with the ID set $set['new_id'] sent from the front desk,

<code>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]." 存在<br>";

                $this->load->view('checkout/v_test');

            }else{
                //将当前数据库中没有的写进数组$checkout[]
                $checkout[$i]=$set_new_id[$i];    

            }
        }
    print_r($checkout);
    
    //$this->m_checkout->add_new_id($checkout);
}</code>
Copy after login
Copy after login

The values ​​I passed in:
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 ) )

Printed:
ID: 123 exists
Array ( [0] => 123 [1] => 1233 )

Question: Why does print_r($checkout); still have 123
I have obviously screened it.
Hope God will tell you. . Thank you

<code>$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...
    }
}</code>
Copy after login

This is an example of using in_array correctly. Think about your specific problem yourself. You can't tell what $set['old_id'] looks like by looking at the code.

First of all, the format of $set_old_id is wrong. In addition, when using the in_array function, you need to pay attention to some of its "weird behaviors". There are many examples in the manual that show what you need to pay attention to when using this function. Please refer to the manual. in_array.

If the format of $set_old_id is correct, the ID will be extracted to form a one-dimensional array, such as: [1,2,3,4]. Then when using the in_array function, you need to use the third parameter here, which means strict matching (the type and value need to be exactly the same). Such as:

<code class="php">in_array($set_new_id[$i], $set_old_id, true)</code>
Copy after login

In this way, we can accurately determine whether a value exists in an array. Of course, other methods can also be used, such as isset() etc. There are many cases in the PHP manual.

I have also been tricked when using in_array() in the past, and I recorded it here "Some "traps" in PHP.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template