PHP Tutorial foreach unset

Source: Internet
Author: User
Tags php tutorial

First, we use an array to simulate a parameter pass. Received is a set of IDs, which I name here $nums:


$nums [] = 1;

$nums [] = 2;

$nums [] = 3;

$nums [] = 4;


We then use the $data array to touch the contents of a data table, with ID, username:


$data [0][' id '] = 1;

$data [0][' name '] = ' aaa ';

$data [1][' id '] = 2;

$data [1][' name '] = ' BBB ';

$data [2][' id '] = 3;

$data [2][' name '] = ' CCC ';

$data [3][' id '] = 4;

$data [3][' name '] = ' ddd ';


Next, we use the $rs array to touch the contents of another table, and correspondingly, with the same ID, and then the table contains the phone number:


$rs [0][' id '] = 1;

$rs [0][' mobile '] = ' 1111111 ';

$rs [1][' id '] = 2;

$rs [1][' mobile '] = ' 2222222 ';

$rs [2][' id '] = 10;

$rs [2][' mobile '] = ' 3333333 ';

$rs [3][' id '] = 4;

$rs [3][' mobile '] = ' 4444444 ';


Now all we have to do is to read out the username and phone number corresponding to the ID from the two arrays of $data and $rs, and display it in the list:


Echo ' <table width=400 align=center border=1> ';


foreach ($nums as $k => $v) {


Simulate reading data from $nums = = $data [' id '] in the database, get user name

foreach ($data as $KD => $vd) {

if ($v = = $VD [' id ']) {

$names = $vd [' name '];

}

}


Simulate reading data in the database $nums = = $rs [' id '], get the cell phone number

foreach ($rs as $kr => $vr) {

if ($v = = $VR [' id ']) {

$mobile = $VR [' Mobile '];

}

}


Echo ' <tr>

<td> '. $v. ' </td>

<td> '. $names. ' </td>

<td> '. $mobile. ' </td>

</tr> ';

}


Echo ' </table> ';

Then, after writing here, I ran this page, but when I ran out of the page, I found that something was wrong:


As shown in the figure, the number of the user ID = = 3 is changed to the number of id = 2.


Why is this? Let's take a look at our code:


First, the "id" in the $nums array is 1,2,3,4 Yes, $data the "id" in the array is also 1,2,3,4 Yes, but there is a problem with the "id" in the $rs array, which is 1,2,10,4. There's No 3 here.


So the phone number is not read. But why does the phone number show the phone number of id = 2 in the column ID = 3? We went on to look down:


Simulate reading data in the database $nums = = $rs [' id '], get the cell phone number

foreach ($rs as $kr => $vr) {

if ($v = = $VR [' id ']) {

$mobile = $VR [' Mobile '];

}

}


We are here to give $mobile the value of the assignment, when the "id" in the $nums equals the "id" in the $RS, it assigns the phone number in the $rs to the $mobile variable, and when the third loop is performed after ID = 2, we find that the condition does not match, so we jump directly It's over. However, the value of the $mobile is still the value of id = = 2! Right. That's the problem!


After I found the problem, I took a rest, drank a sip of cold water, and gave all the loop variables to unset () before the end of the foreach statement.

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.