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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作