Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

laravel - two json data are the same, no need to display

here I have two json data, in the json data in model1 judul_kontrak and model2 nama_proyek there are two the same data aa, aa and cc data in model2 do not need to be displayed, only model1 data is displayed which are not the same.

the exit should only be bb from model1

$model1 = [
    [
        'judul_kontrak' => 'aa',
        'kode' => '01'
    ],
    [
        'judul_kontrak' => 'bb',
        'kode' => '02'
    ]
];
$model2 = [
    [
        'nama_proyek' => 'aa',
        'kode' => '05'
    ],
    [
        'nama_proyek' => 'cc',
        'kode' => '06'
    ]
];
$arr = [];
$proyek = [];
foreach ($model1 as $m1) {
    $proyek['nama_proyek'] = $m1['judul_kontrak'];
    foreach($model2 as $m2){
        if(trim(strtolower($m1['judul_kontrak'])) == trim(strtolower($m2['nama_proyek']))){
            $proyek = [];
        }
    }
    $arr[] = $proyek;
}
return $arr;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use laravel collect() function

$model1 = [
    [
        'judul_kontrak' => 'aa',
        'kode' => '01'
    ],
    [
        'judul_kontrak' => 'bb',
        'kode' => '02'
    ]
];
$model2 = collect([
    [
        'nama_proyek' => 'aa',
        'kode' => '05'
    ],
    [
        'nama_proyek' => 'cc',
        'kode' => '06'
    ]
]);
$arr = [];
        
foreach ($model1 as $item) {
    $val = $item['judul_kontrak'];
    $check = $model2->where('nama_proyek', $val)->count();
    if (!$check) {
        $arr[] = $item;
    }
}
return $arr;

it result is

[{
    "judul_kontrak": "bb",
    "kode": "02"
}]

it only return $model1 data which is not present in model2's nama_proyek


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...