[Array] What is the error?
"Reunion", "07-21" => "credit and loan repayment", "07-23" => "purchasing products"); $ date = "07-23 "; foreach ($ rc as $ key => $ value) {if ($ key = $ date) {echo "script alert ('". $ key. "Memo :". $ value. "')" script "; break;} else {echo" "script" alert ('No memos') "script"; break ;}}?>
If the third line is: $ date = "07-12", a prompt box is displayed: 07-12 memo: Reunion
If the third line is: $ date = "07-21", or: $ date = "07-23", it should be the same as above, but the prompt box is displayed: no memo
Where is the error?
Reply to discussion (solution)
Do you have a break? the traversal will exit once.
On the same floor, line 3 cannot break
$ Rc = array ("07-12" => "reunion", "07-21" => "credit repayment ", "07-23" => "buy item"); $ date = "07-23"; $ msg = ''; foreach ($ rc as $ key => $ value) {if ($ key = $ date) {$ msg = "script alert ('". $ key. "Memo :". $ value. "') script"; break;} if (! $ Msg) $ msg = "script" alert ('No memos ') script "; echo $ msg;Think about why
$ Rc = array ("07-12" => "reunion", "07-21" => "credit repayment ", "07-23" => "buy item"); $ date = "07-23"; $ msg = ''; foreach ($ rc as $ key => $ value) {if ($ key = $ date) {$ msg = "script alert ('". $ key. "Memo :". $ value. "') script"; break;} if (! $ Msg) $ msg = "script" alert ('No memos ') script "; echo $ msg;Think about why
Positive solution. Thank you.
But why is it written like this? I haven't figured it out yet.
Do you understand this?
$ Rc = array ("07-12" => "reunion", "07-21" => "credit repayment ", "07-23" => "purchasing products"); $ date = "07-23"; $ msg = "script" alert ('No resilies ') script "; // give an initial value foreach ($ rc as $ key => $ value) {if ($ key = $ date) {// If yes, modify $ msg = "script alert ('". $ key. "Memo :". $ value. "') script"; break; // just to end the loop in advance} echo $ msg;
Xuzuning is easy to use, and the code on the 6th floor is easy to understand.
I used netbeans-xdebug to run the code on the first floor. I checked the variable value changes in one step and understood the error of the code on the first floor.
First floor code if the third line is: $ date = "07-21"; the first cycle, $ key is 07-12, not equal to $ date07-21, in this case, the js prompt box is "no memo" instead of "-21: Credit repayment ".
The correct method is that a prompt box is displayed only when $ key = $ date.
In this case, there cannot be two echo sets in the next loop.
A basic problem. if you don't pay attention to it, it will actually go around!