PHP compares the problematic values of two arrays to determine the output results. The two groups of data are retrieved from the two tables and saved to $ mall_tems and $ mall_tems respectively.
Now we need to traverse and compare two arrays. if the color_id and size_id of the record are the same, the price value of $ mall will be taken out and saved to the price field of $ malls_tem.
Now we use the following foreach loop, but it is not successful. Please help us to see what is wrong.
Foreach ($ mall_tems as $ mall_tem) {// Laravel used, $ current_mall indicates getting the data of the current loop. $ Current_mall = find ($ mall_tem-> id); foreach ($ Mallas $ mall) {if ($ mall_tem-> color_id ==$ mall-> color_id & $ mall_tem-> size_id ==$ mall-> size_id) {$ current_mall-> price = $ mall-> price;} else {$ current_mall-> price = '2017. 00 ';}}}
The problem is that $ mballs cannot be traversed normally. $ mall_tems price data can be written, but it is incorrect.
Thank you.
Reply to discussion (solution)
Except for the unknown meaning of $ current_mall, there is no problem.
Except for the unknown meaning of $ current_mall, there is no problem.
Obtain the product record of the current loop, and store the if statement to determine the output price to this record. There is another $ current_mall-> save (); skipped,
It is also normal during the individually segmented printing test. Loop writes are messy and there are multiple qualified records. However, only the first record can be normally written to the if clause to determine the price, and the other values are else prices.
Your $ current_mall is a single object, and it is always assigned a value in the internal loop.
The key point is omitted when you omit the code: $ current_mall-> save () where to execute
So what can't be clearly stated?
In general, since a value is required for non-matching, it can be understood that $ current_mall has an initial value.
$ Current_mall-> price = '2017. 00'; foreach ($ Mallas $ mall) {if ($ mall_tem-> color_id ==$ mall-> color_id & $ mall_tem-> size_id ==$ mall-> size_id) {$ current_mall-> price = $ mall-> price; break; // The match ends. }}$ current_mall-> save ();
Your $ current_mall is a single object, and it is always assigned a value in the internal loop.
The key point is omitted when you omit the code: $ current_mall-> save () where to execute
So what can't be clearly stated?
In general, since a value is required for non-matching, it can be understood that $ current_mall has an initial value.
$ Current_mall-> price = '2017. 00'; foreach ($ Mallas $ mall) {if ($ mall_tem-> color_id ==$ mall-> color_id & $ mall_tem-> size_id ==$ mall-> size_id) {$ current_mall-> price = $ mall-> price; break; // The match ends. }}$ current_mall-> save ();
Add break; yes, it turns out to overwrite the data!
Thank you for saving your life.
Thanks again!