Cakephp检查两个值是否在数组中

时间:2010-01-16 21:40:35

标签: php cakephp

如果他们住在住宿区并且member_id与Auth->用户('id')匹配,我只需要允许会员在住宿上撰写评论。

在住宿/视图中,我传递了BookingRequests数据。

看起来像这样

BookingRequest
  0
    member_id => 4
    accepted => 1
  1
    member_id => 5
    accepted => 0
  2
    member_id => 4
    accepted => 0

我如何搜索数组,以便只允许留下来的人添加评论?

3 个答案:

答案 0 :(得分:0)

在其他语言的PHP中并不那么容易,所以你必须以不同的方式注入。

这样的事情:

$vars = array();
foreach ($members as $request) {
  if ($request->accepted) {
    array_push($vars, $request);
  }
}

答案 1 :(得分:0)

试试这个,

function search($array, $key, $value)
    {
        $results = array();

        if (is_array($array))
        {
            if ($array[$key] == $value)
                $results[] = $array;

            foreach ($array as $subarray)
                $results = array_merge($results, search($subarray, $key, $value));
        }

        return $results;
    }

    $array = array(
                  "member_id" => 4, array("accepted" => 11),
                  "member_id" => 5, array("accepted" => 02), 
                  "member_id" => 4, array("accepted" => 0)
                   );


    $stayedmember = search($array,$memberID,"accepted");

    print_r($stayedmember );

答案 2 :(得分:0)

假设'已接受'确定他们是否留在住宿区,并且下面的测试数组确实是您的数据阵列的格式,那么您可以使用以下代码:

/* Test data */
$BookingRequestData = array(
    array('member_id' => 4, 'accepted' => 1),
    array('member_id' => 5, 'accepted' => 0),
    array('member_id' => 4, 'accepted' => 0)
);

$canAddReview = (count(Set::extract("/data[member_id=" . $this->Auth->user('id') . "][accepted=1]", array('data' => $BookingRequestData))) > 0);