PHP: How to compare the problematic values of two arrays to determine the output-php Tutorial

Source: Internet
Author: User
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!

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.