PHP about comparing the problem value of two arrays to determine the output problem

Source: Internet
Author: User
Take two sets of data from the two tables and deposit them $malls and $mall separately _tems

It is now necessary to traverse the comparison of two arrays, and if the record color_id and size_id are the same, take out the price value of the $mall and deposit it in the Price field of the $malls _tem

Now using the following foreach loop, but not successful, please help to see what is wrong.
foreach ($mall _tems as $mall _tem) {    //Laravel used, $current _mall represents the data for the current loop.    $current _mall = Find ($mall _tem->id);     foreach ($malls as $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 = ' 129.00 ';}}    

The problem now is that $malls cannot traverse normally, $mall _tems price data can be written, but not correctly.

Thank you.


Reply to discussion (solution)

There is no problem except $current _mall meaning

There is no problem except $current _mall meaning



Gets the current cycle of the product record, convenient deposit if the output of the price to determine the record. There is another $current _mall->save ();

Individually segmented print tests are also normal. The loop write is chaotic, there are multiple qualifying records, but only the first one can be written correctly if the price is judged, the other is to be fixed in the else price.

Your $current _mall is a single object and always assigns values in the inner loop
Since you omitted the code, the key points were omitted: where $current _mall->save () is executed
So it's not clear what to say

As a general practice, since the mismatch is also to be assigned value, it can be understood as $current _mall is the initial value

    $current _mall->price = ' 129.00 ';    foreach ($malls as $mall)    {        if ($mall _tem->color_id = = $mall->color_id && $mall _tem->size_id = = $mall->size_id)         {            $current _mall->price = $mall->price;            Break Match to the end        }    }    $current _mall->save ();

Your $current _mall is a single object and always assigns values in the inner loop
Since you omitted the code, the key points were omitted: where $current _mall->save () is executed
So it's not clear what to say

As a general practice, since the mismatch is also to be assigned value, it can be understood as $current _mall is the initial value

    $current _mall->price = ' 129.00 ';    foreach ($malls as $mall)    {        if ($mall _tem->color_id = = $mall->color_id && $mall _tem->size_ id = = $mall->size_id)         {            $current _mall->price = $mall->price;            Break Match to the end        }    }    $current _mall->save ();




Plus break, you can, the original data again covered!
Thank you, Hero saving.
Thanks again!
  • Related Article

    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.