检索数组中的匹配元素

时间:2012-07-03 19:06:27

标签: php arrays

我有一个像这样的数组

Array
(
    [35635d5ebdd938d6360e65a9e2484073] => Array
        (
            [rowid] => 35635d5ebdd938d6360e65a9e2484073
            [id] => MYU_SC3
            [qty] => 1
            [price] => 4800
            [name] => JAMB UTME Scratch Card
            [service_image] => assets/img/jamb.jpg
            [service_category] => cards
            [subtotal] => 4800
        )

    [d8df18561040f3d9bd9868f5c5aaa7c2] => Array
        (
            [rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
            [id] => MYU_SC1
            [qty] => 1
            [price] => 1600
            [name] => WAEC Scratch Card
            [service_image] => assets/img/waec.jpg
            [service_category] => cards
            [subtotal] => 1600
        )

    [a4a751dd9a69824eb3abb6f49c7a7f61] => Array
        (
            [rowid] => a4a751dd9a69824eb3abb6f49c7a7f61
            [id] => MYU_SC2
            [qty] => 1
            [price] => 1600
            [name] => NECO Scratch Card
            [service_image] => assets/img/neco.jpg
            [service_category] => cards
            [subtotal] => 1600
        )

)

我想检索与另一个数组中的元素匹配的子数组

$card_skus = array("MYU_SC1","MYU_SC2","MYU_SC3");

循环遍历主数组

foreach ($this->cart->contents() as $key => $item) {
      if(in_array($item['id'], $card_skus))
      {
       //Didn't know what to do at this point
      }
}

如何帮助我完成这项工作

1 个答案:

答案 0 :(得分:0)

我想@nickb已在评论中回答,在写答案时没有看到它。

试试这个

$subArray = array();
foreach ($this->cart->contents() as $key => $item) {
   if(in_array($item['id'], $card_skus))
   {
      //Didn't know what to do at this point
      $subArray[] = $item;
   }
}

根据您的要求立即使用$ subArray