简体   繁体   English

比较来自一个数组PHP的数组值

[英]Compare array values from one array PHP

NOTE: 注意:

I'm new to php and mysql. 我是php和mysql的新手。

Background info: 背景信息:

I have +/- 30,000 products and they are from 7 different supplier and they have a lot of the same products 我有+/- 30,000种产品,它们分别来自7个不同的供应商,并且它们有很多相同的产品

let's say i have 1 product and three of the suppliers have the it, i need to publish the product with the lowest price and the other two product stay unpublished 假设我有1个产品,三个供应商都有,我需要以最低的价格发布该产品,而其他两个产品则保持未发布状态

that is the basic idea and this must run through the 30,000 products and check and see if there are any matches and run the publishing function 这是基本思想,必须运行30,000种产品,并检查是否存在匹配项并运行发布功能

SQL setup: SQL设置:

There are two tables xxxx_virtuemart_product and xxxx_virtuemart_product_prices 有两个表xxxx_virtuemart_productxxxx_virtuemart_product_prices

There are three rows in xxxx_virtuemart_product ▬▬▬ product_id,product_sku,published ▬▬▬ xxxx_virtuemart_product▬▬▬product_id,product_sku,已发布▬▬▬中有三行

There are two rows in xxxx_virtuemart_product_prices ▬▬▬ product_id,product_price ▬▬▬ xxxx_virtuemart_product_prices▬▬▬product_id,product_price are中有两行

My little bit of code: 我的一点点代码:

I have this little code because i'm stuck, how can i make a check to see if there are any matches and then run a query to change the published value of the product with the lowest price? 我有这个小代码,因为我被卡住了,如何检查是否有匹配项,然后运行查询以更改价格最低的产品的发布值?

i know there is a way to use the query to check for matches, but do not understand how to do is 我知道有一种方法可以使用查询来检查匹配项,但是不知道该怎么做是

$query = "SELECT `product_sku` FROM `xxxx_virtuemart_product`";
$query_run = mysql_query($query) or die (mysql_error());

while($row = mysql_fetch_assoc($query_run)){
    foreach ($row as $key => $ps){

    }
}

The below code is to check the price (not query optimize just a rough draft) 下面的代码是检查价格(不是查询优化只是草稿)

$z = //products price;
$x = //products price;
$c = //products price;

if ($z >= $x && $c >= $x) {

    //the this products published value to 1

}else if ($x >= $z && $c >= $z) {

    //the this products published value to 1

}else if ($z >= $c && $x >= $c) {

    //the this products published value to 1

}

Can anyone please help me with this? 谁能帮我这个忙吗?

Thanks for reading 谢谢阅读

any questions are welcome. 任何问题都欢迎。

Your problem is not about to compare arrays but more algorithm. 您的问题不是要比较数组,而是要更多的算法。 I will try this way. 我会尝试这种方式。 1)SQL query to retrieve all the products : sql_products 2)construct a hashmap those key is product SKU (see : PHP Array ) 1)SQL查询以检索所有产品:sql_products 2)构造一个哈希图,这些键是产品SKU(请参阅: PHP Array

To construct PHP hashmap : use arrays of arrays. 构造PHP hashmap:使用数组数组。 Example : product_map[] = array(); 示例:product_map [] = array();

foreach sql_product of sql_products : - product_map[sql_product[SKU]][] = array(sql_product[supplier], sql_product[price], sql_product[information]) end of loop foreach sql_products的sql_product:-product_map [sql_product [SKU]] [] =数组(sql_product [供应商],sql_product [价格],sql_product [信息])

Then loop again over the map of products constructed and sort each product record by price : that means you have to write a function to sort array (supplier, price, information). 然后再次遍历所构造的产品地图,并按价格对每个产品记录进行排序:这意味着您必须编写一个对数组(供应商,价格,信息)进行排序的函数。

Now you have a map of product : SKU => array(of array (supplier, price, information)) 现在您有一个产品图:SKU => array(of array(供应商,价格,信息)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM