PHP 关于比较两个数组的问题值判断输出的问题
从两个表中取出两组数据,分别存入 $malls 和 $mall_tems
现在需要遍历比较两个数组,如果记录的 color_id 和 size_id 相同,就取出 $mall 的 price 值存入 $malls_tem 的 price 字段中
现在使用以下 foreach 循环,但是不成功,麻烦各位帮忙看看哪里不对。
foreach($mall_tems as $mall_tem){ // 使用的 Laravel,$current_mall 就代表获取当前循环的数据吧。 $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'; } }}
现在的问题是 $malls 不能正常遍历,$mall_tems price数据可以写入,但是不正确。
谢谢。
回复讨论(解决方案)
除了 $current_mall 含义不明外,并无什么问题
除了 $current_mall 含义不明外,并无什么问题
获取当前循环的商品记录,方便存入 if 判断输出的 price 到这条记录中。后边还有个 $current_mall->save();省略了,
单独分段打印测试时也正常。循环写入时就乱了,有多个符合条件的记录,但是,只有第一条可以正常的写入 if 判断出的价格,其它的定入的都是 else 价格。
你的 $current_mall 是单个对象,并且总是在内层循环里赋值
由于你省略代码时把关键点省略了:$current_mall->save() 在哪里执行的
所以不能明确的说什么
按一般的做法,既然不匹配时 也是要赋值的,就可理解为 $current_mall 是有初值的
$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; //匹配到就结束 } } $current_mall->save();
你的 $current_mall 是单个对象,并且总是在内层循环里赋值
由于你省略代码时把关键点省略了:$current_mall->save() 在哪里执行的
所以不能明确的说什么
按一般的做法,既然不匹配时 也是要赋值的,就可理解为 $current_mall 是有初值的
$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; //匹配到就结束 } } $current_mall->save();
加上break;可以了,原来把数据又覆盖了!
多谢大侠救命之恩。
再次感谢!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
