php 双重循环优化 foreach

数据越庞大效果越明显 

没看懂请留言

$VW_SHANGPIN_FJSX=[
        ['spdm'=>'kf','spmc'=>'咖啡','type'=>1],
        ['spdm'=>'kl','spmc'=>'可乐','type'=>2],
        ['spdm'=>'nn','spmc'=>'牛奶','type'=>1]
    ];
$SHANGPIN=[
        ['spdm'=>'kf','zjf'=>'kafei','jg'=>99],
        ['spdm'=>'kl','zjf'=>'kele','jg'=>3],
        ['spdm'=>'nn','zjf'=>'niunai','jg'=>10]
    ];

// 双重循环
foreach($VW_SHANGPIN_FJSX as $k=>$v){
    foreach($SHANGPIN as $ke=>$va){
        if($v['spdm']==$ke['spdm']){
            $VW_SHANGPIN_FJSX[$k]['zjf']=$SHANGPIN[$ke]['zjf'];
            $VW_SHANGPIN_FJSX[$k]['jg']=$SHANGPIN[$ke]['jg'];
        }
    }
}

// 双重循环优化
function for_youhua($column_array,$walk_array,$v,$v_k){
    $zjf=array_column($column_array,$v,$v_k);
    array_walk($walk_array, function(&$val,$key,$arr) use($zjf) {
        $val[$arr[1]] = $zjf[$val[$arr[0]]];
        return $val;
    },[$v_k,$v]);
    return $walk_array;
}

//调用优化双重循环
$VW_SHANGPIN_FJSX=for_youhua($SHANGPIN,$VW_SHANGPIN_FJSX,'zjf','spdm');
$VW_SHANGPIN_FJSX=for_youhua($SHANGPIN,$VW_SHANGPIN_FJSX,'jg','spdm');
var_dump($VW_SHANGPIN_FJSX);die;

 

你可能感兴趣的:(PHP,php,双重循环优化,php,循环优化,php,大数据循环优化)