foreach循环取出$rows_order 中的值,再将需要的id的值join成字符串,方便查询数据
rows_order 的值:
Array
(
[0] => Array
(
[order_id] => 52
[order_order_id] => 26
[order_product_id] => 1
)
[1] => Array
(
[order_id] => 53
[order_order_id] => 26
[order_product_id] => 4
)
[2] => Array
(
[order_id] => 54
[order_order_id] => 26
[order_product_id] => 19
)
)
$arr=array();
foreach ($rows_order as $value) {
array_push($arr,$value['order_product_id']);
}
$str_product_id = "'" . join("','", array_values($arr) ) . "'";
$str_order_id的值为
'1','4','19'
使用查出的id查询
$where_re = " where 1= 1 ";
$where_re .= " and product_id in(".$str_product_id .")";
$where = " and prod_id in(".$str_product_id .")";
//分两次查询两个表,减少 LEFT JOIN 的使用
$rows_sproduct = mod_product::get_product_sdata($where);
$rows_product = mod_product::get_product_data($where_re);
rows_sproduct的数据:
Array
(
[0] => Array
(
[s_id] => 1
[noff] => 1
[mend_te] => 0
[nnel_id] => 4
[prod_id ] => 1
)
[1] => Array
(
[s_id] => 3
[noff] => 1
[mend_te] => 0
[nnel_id] => 4
[prod_id ] => 4
)
[2] => Array
(
[s_id] => 13
[noff] => 1
[mend_te] => 0
[nnel_id] => 4
[prod_id ] => 19
)
)
rows_product 的数据:
Array
(
[0] => Array
(
[product_id] => 1
[proryid] => 1
)
[1] => Array
(
[product_id] => 4
[proryid] => 1
)
[2] => Array
(
[product_id] => 19
[proryid] => 1
)
)
//将两个数组合并
foreach($rows_product as &$arr_product){
foreach($rows_sproduct as $arr_value){
//当两个id相同时合并该数据
if($arr_value['prod_id']==$arr_product['product_id']){
$rows[] = array_merge($arr_product,$arr_value);
}
}
}
最后得出$rows的数据
Array
(
[0] => Array
(
[product_id] => 1
[proryid] => 1
[s_id] => 1
[noff] => 1
[mend_te] => 0
[nnel_id] => 4
[prod_id ] => 1
)
[1] => Array
(
[product_id] => 4
[proryid] => 1
[s_id] => 3
[noff] => 1
[mend_te] => 0
[nnel_id] => 4
[prod_id ] => 4
)
[2] => Array
(
[product_id] => 19
[proryid] => 1
[s_id] => 13
[noff] => 1
[mend_te] => 0
[nnel_id] => 4
[prod_id ] => 19
)
)