PHP foreach循环取出数组数据,并合并二维数组

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
        )

)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值