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!