如何比较PHP中来自不同foreach循环的两个值?

时间:2018-07-30 07:21:37

标签: php mysql

这是我的密码,

我的问题是,来自前一个的$company_id是否等于来自前两个的$Company_id,然后是echo company_name

$ids = array();
$x = array();
$a = array();

foreach($companieslist as $keys=>$company) {
    $x[$company->company_id] = [
        'id' => $company->company_id,
        'name' => $company->company_name
    ];
}
$entry = $a[$id];

foreach($uploads as $keys=>$general){
    $ids[] = $general->Contract_Id;
    $c_id = $general->Company_id;
    ....

1 个答案:

答案 0 :(得分:0)

仅仅从性能方面来说,您应该做的就是先将第二批的公司ID提取到数组中,就像这样

$companies = array();
foreach ( $uploads as $keys => $general ) {
    array_push( $companies, $general->Company_id );
}

现在,在第一个foreach循环中,您只需检查该$ companies数组中是否存在公司ID,然后决定要做什么

foreach($companieslist as $keys=>$company){
    if(in_array($company->company_id,$companies)){
        echo "Found {$company->company_id}<br/>\n";
    }
}