dougaxing8673 2017-10-03 18:02
浏览 43
已采纳

动态检查PHP数组中的字符串

EDIT: ten months later, I'm still back to this.. still can't figure it out :(

I can search for a string in an array no problem; this works:

if (in_array('animals', $value[tags])){
   echo "yes";
}

But how can I check for a variable in the array? This doesn't seem to work:

$page_tag = 'animals';
if (in_array($page_tag, $value[tags])){
   echo "yes";
}

I'm guessing I'm missing some simple syntax doodad?

The array is massive, so I'll try and show a sample of it. It is stored on a separate php file and "included" in other places.

global $GAMES_REPOSITORY;
$GAMES_REPOSITORY = array   (
     "Kitten Maker"      => array   (
        "num"   => "161",
        "alt"   => "Kitten Maker animal game",
        "title" => "Create the kitten or cub of your dreams!",
        "tags"  => array ("animals", "feline", "cats", "mega hits"),
     ),
}

Here's a larger part of the code, to put into context. It pulls from the array of ~400 games, and pulls the ones with a specific tag:

function array_subset($arr) {
    $newArray = array();
    foreach($arr as $key => $value) {
        if (in_array($page_tag, $value["tags"])){
            if(is_array($value)) $newArray[$key] = array_copy($value);
            else if(is_object($value)) $newArray[$key] = clone $value;
            else $newArray[$key] = $value;
        }
    }
    return $newArray;
}
function array_copy($arr) {
    $newArray = array();
    foreach($arr as $key => $value) {
        if(is_array($value)) $newArray[$key] = array_copy($value);
        else if(is_object($value)) $newArray[$key] = clone $value;
        else $newArray[$key] = $value;
    }
    return $newArray;
}
$games_list = array();
$games_list = array_subset($GAMES_REPOSITORY);
$games_list = array_reverse($games_list);

Oh, an interesting hint. Elsewhere it DOES work using $_GET:

if (in_array($_GET[tagged], $value[tags])){
  • 写回答

3条回答 默认 最新

  • douchong4730 2018-08-10 18:57
    关注

    I finally got it to work! I don't entirely understand why, but I had to feed the variable into the function directly. For some reason it wouldn't pull the variable from the parent function. But now it works and it even takes two dynamic variables:

    function array_subset2($arr, $tag1, $tag2) {
        $newArray = array();
        foreach($arr as $key => $value) {
            if (in_array($tag1, $value['tags'])){
                if (in_array($tag2, $value['tags'])){
                    if(is_array($value)) $newArray[$key] = array_copy2($value);
                    else if(is_object($value)) $newArray[$key] = clone $value;
                    else $newArray[$key] = $value;
                }
            }
        }
        return $newArray;
    }
    function array_copy2($arr) {
        $newArray = array();
        foreach($arr as $key => $value) {
            if(is_array($value)) $newArray[$key] = array_copy2($value);
            else if(is_object($value)) $newArray[$key] = clone $value;
            else $newArray[$key] = $value;
        }
        return $newArray;
    }
    $games_list = array();
    $games_list = array_subset2($GAMES_REPOSITORY, $page_tag, $featured_secondary_tag);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂