如何比较php中不同foreach循环的两个值?

rxztt3cl  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(403)

这是我的密码,
我的问题是 $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;
    ....
b1payxdu

b1payxdu1#

从性能方面讲,您应该首先将第二批中的公司id提取到一个数组中,如下所示

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

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

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

相关问题