【问题标题】:lumen, how can I not display the same name?lumen,怎么不能显示同名?
【发布时间】:2021-03-26 17:58:00
【问题描述】:

如果第二个foreach有同名的话我有代码,那么同名不显示,但是不知道怎么保持同名不显示?。

$arr = [];
foreach ($respon_tel['item'] as $item) {
     $proyek = [
          'nama_proyek' => $item['judul_kontrak'],
          'from' => 'Another API'
     ];
     foreach($model as $m){
          if(trim(strtolower($item['judul_kontrak'])) == trim(strtolower($m['nama_proyek']))){
          // ????
          }
      }
$arr[] = $proyek;
}
return $arr;

【问题讨论】:

    标签: php laravel api lumen


    【解决方案1】:

    您可以从$model 中获取所有nama_proyek,然后您可以检查当前$item 的judul_kontrak 是否在该集合中:

    $models = collect($model)
        ->map(fn ($i) => trim(strtolower($i['nama_proyek'])));
    
    foreach ($respon_tel['item'] as $item) {
        if (! $models->contains(trim(strtolower($item['judul_kontrak'])))) {
            $arr[] = [
                'nama_proyek' => $item['judul_kontrak'],
                'from' => 'Another API'
            ];
        }
    }
    

    或者您可以使用 Collection 方法获得创意:

    collect($respon_tel['item'])
        ->pluck('judul_kontrak', 'judul_kontrak')
        ->map($f = fn ($item) => trim(strtolower($item)))
        ->diff(
            collect($model)->pluck('nama_proyek')->map($f)
        )->map(function ($item, $key) {
            return [
                'nama_proyek' => $key,
                'from' => 'Another API',
            ];
        })->values();
    

    【讨论】:

      猜你喜欢
      • 2011-08-11
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 2013-09-12
      • 2012-10-16
      • 1970-01-01
      相关资源
      最近更新 更多