0

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])){
2
  • show your array Commented Oct 3, 2017 at 18:05
  • @iCoders I added a piece of the array to the question Commented Oct 3, 2017 at 18:29

3 Answers 3

1

The in_array() function can check variables, so it is likely that your problem comes from somewhere else. Verify that you've defined your constant tags correctly. If it's not defined, it might not work depending on your PHP version. Some versions just assume that you wanted to write the string tags instead of a constant named tags.

Your code works. Here's a full example that I've tested that works well:

<?php
const tags = "tags";
$page_tag = 'animals';
$value = array('tags' => array("fruits", "animals"));

if (in_array($page_tag, $value[tags])){
   echo "yes";
}
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks @Brian I added a piece of the array to the question. Okay, good to know that the code works. I'm trying to figure out how to incorporate your answer...
Have you tried writing $value["tags"] instead of $value[tags] ?
Trying now. No, didn't work. I assumed that part wasn't the problem because in_array('animals', $value[tags]) does work...?
Well, your syntax is correct. So, it's likely that the problem comes from somewhere else. Maybe try it that way without the constants... if (in_array($page_tag, $GAMES_REPOSITORY["Kitten Maker"]["tags"]))
Thank you. I can't quite word it like that because it's nested in a loop, in a function. But I think that's basically what it's doing now. I've added more of the code... maybe I've messed something up further up the function :/
|
1

You have an array of arrays, so in_array() wont work as you have written it as that test for existence in an array, not a subarray. You may as well just loop through your arrays like this:

foreach($GAMES_REPOSITORY as $name =>$info) {
   if(in_array($page_tag, $info['tags']))
   {  whatever  } 
}

If that is not fast enough you will have to cache your tags by looping ahead of time and creating an index of tags.

1 Comment

Isn't that what I'm doing though? I can't see significant differences between your code and mine.. (i appended more of the code near the bottom of the question later)
1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.