数组只匹配一个结果

时间:2012-11-18 15:48:06

标签: php arrays match

是否有可能实现这个目标?

我有一些数组

Array
(
    [1] => Model_Like Object
        (
            [_is_new:protected] => 
            [_frozen:protected] => 
            [_data:protected] => Array
                (
                    [id] => 1
                    [user_id] => 5
                    [liked_by] => 1
                )

            [_custom_data:protected] => Array
                (
                )

            [_original:protected] => Array
                (
                    [id] => 1
                    [user_id] => 5
                    [liked_by] => 1
                )

            [_data_relations:protected] => Array
                (
                )

            [_original_relations:protected] => Array
                (
                )

            [_reset_relations:protected] => Array
                (
                )

            [_view:protected] => 
            [_iterable:protected] => Array
                (
                )

        )

    [2] => Model_Like Object
        (
            [_is_new:protected] => 
            [_frozen:protected] => 
            [_data:protected] => Array
                (
                    [id] => 2
                    [user_id] => 5
                    [liked_by] => 2
                )

            [_custom_data:protected] => Array
                (
                )

            [_original:protected] => Array
                (
                    [id] => 2
                    [user_id] => 5
                    [liked_by] => 2
                )

            [_data_relations:protected] => Array
                (
                )

            [_original_relations:protected] => Array
                (
                )

            [_reset_relations:protected] => Array
                (
                )

            [_view:protected] => 
            [_iterable:protected] => Array
                (
                )

        )

    [4] => Model_Like Object
        (
            [_is_new:protected] => 
            [_frozen:protected] => 
            [_data:protected] => Array
                (
                    [id] => 4
                    [user_id] => 5
                    [liked_by] => 6
                )

            [_custom_data:protected] => Array
                (
                )

            [_original:protected] => Array
                (
                    [id] => 4
                    [user_id] => 5
                    [liked_by] => 6
                )

            [_data_relations:protected] => Array
                (
                )

            [_original_relations:protected] => Array
                (
                )

            [_reset_relations:protected] => Array
                (
                )

            [_view:protected] => 
            [_iterable:protected] => Array
                (
                )

        )

)

我只希望显示一个匹配的结果。

<?php foreach ($user->likes as $likes): ?>
<?php if($likes['liked_by'] == $logged_in_user_id): ?>
     <?php echo "you like it" ?>
<?php else: ?>
    <?php echo "you dont like it" ?>
<?php endif; ?>
<?php endforeach; ?>

这样我得到以下结果

you like it  you dont like it    you dont like it

是否仅显示匹配结果?

3 个答案:

答案 0 :(得分:1)

使用从循环中跳出的return;

<?php 
   $result = "you dont like it";
   foreach ($user->likes as $likes) {
     if($likes['liked_by'] == $logged_in_user_id) {
       $result = "you like it";
     }
   }
   echo $result;
?>

答案 1 :(得分:0)

当你击中比赛时,你可以跳出循环。有很多种方法可以实现这一点,从使用标志变量到打破匹配控制流程。

答案 2 :(得分:0)

或者,您可以过滤喜欢的数组以仅包含用户的喜欢。之后,你只需要获取该数组的第一个条目,这是第一个“喜欢”。给你一个印象:

$likesOfUser = array_filter($user->likes, function($c) use ($logged_in_user_id)
{
    return $c['liked_by'] == $logged_in_user_id;
});

$like = array_shift($likesOfUser);

但是这可能有点超过顶部而且只是推荐,如果你在单独的数组中需要喜欢的话。