Laravel比较数据库和api

时间:2018-04-12 07:22:24

标签: php laravel

我有api基础数据,如:

array:21 [▼
  0 => "jne"
  1 => "pos"
  2 => "tiki"
  3 => "esl"
  4 => "pcp"
  5 => "rpx"
  6 => "cahaya"
  7 => "dse"
  8 => "first"
  9 => "indah"
  10 => "jet"
  11 => "jnt"
  12 => "ncs"
  13 => "pahala"
  14 => "pandu"
  15 => "sap"
  16 => "sicepat"
  17 => "slis"
  18 => "star"
  19 => "nss"
  20 => "wahana"
]

我得到了这段代码:

$cori = $rajaongkir->courier('all');
$cori =  array_keys($cori);

我还有一个名为couriers的数据库表,其中我保存了same names as you see in my dd above + status,而我的表的dd是:

Collection {#825 ▼
  #items: array:19 [▼
    0 => "jne"
    1 => "pos"
    2 => "tiki"
    3 => "esl"
    4 => "rpx"
    5 => "cahaya"
    6 => "dse"
    7 => "first"
    8 => "indah"
    9 => "jet"
    10 => "jnt"
    11 => "ncs"
    12 => "pahala"
    13 => "pandu"
    14 => "sap"
    15 => "sicepat"
    16 => "slis"
    17 => "star"
    18 => "nss"
  ]
}

这就是我如何做到的:

$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier');
  

PS:正如你看到我的表dd的2名字少于我的api dd那个   因为这两个名称的活动状态为0(已停用)

问题

我在这里尝试做的是比较我的API courier names和我的Database courier names(顺便说一下),然后只返回状态为1的快递员。数据库(忽略我api中的其他快递名称)

我该怎么做比较?

我的完整代码是:

$cori = $rajaongkir->courier('all'); // get all couriers from api

$cori =  array_keys($cori); // retrieve only their names

$selectedcouriers = Courier::where('active', '=', '1')->pluck('courier'); // get my DB couriers with active status

//comparing code...?

1 个答案:

答案 0 :(得分:2)

简单,使用array_intersect()

$result = array_intersect($selectedcourier, $cori);