0

I can't get in_array to work. Its running inside a function which is defined in a file I have included, and then call the function from the original file... This is getting too complex, I will add the code:

index.php

$results = $database->get_results($query);
foreach ($results as $question) {
    echo '<div class="'.addQuestionClass($question).'"></div>';
}

functions.php

$sectionConfig = array(
    'amount' => '20',
    'types' => array('textarea', 'text', 'select', 'number'),
);

function addQuestionClass($question) {
    if(is_array($question)) {
        $order = 'order-'.$question['order'];
        $id = ' question-'.$question['id'];

        if(in_array($question['answer'], $sectionConfig['types'])) {
            $answertype = ' type-'.$question['answer'];
            echo 'true';
        } else {
            $answertype = null;
            echo 'false';
        }

        return $answertype;
    } else {
        return;
    }
}

The problem code is in my addClass function:

in_array($question['answer'], $sectionConfig['types'])

If I run the same code, with the array from $sectionConfig pasted in, like below, it works fine, but it never recognises in the format I have it above.

this works:

in_array($question['answer'], array('textarea', 'text', 'select', 'number'))

user2992596
  • 181
  • 2
  • 12
  • 2
    Your function `addQuestionClass` has no access to `$sectionConfig` array at all. How can it work if it can't even read the values? – N.B. Feb 12 '14 at 09:24

2 Answers2

3

You are accessing $sectionConfig inside your function. By default, this is another scope and the code in your function doesn't know that $sectionConfig exists.

You can try something like this:

$sectionConfig = array(
    'amount' => '20',
    'types' => array('textarea', 'text', 'select', 'number'),
);

$results = $database->get_results($query);
foreach ($results as $question) {
    echo '<div class="'.addQuestionClass($question,$sectionConfig).'"></div>';
}

function addQuestionClass($question,$sectionConfig) {
    if(is_array($question)) {
        $order = 'order-'.$question['order'];
        $id = ' question-'.$question['id'];

        if(in_array($question['answer'], $sectionConfig['types'])) {
            $answertype = ' type-'.$question['answer'];
            echo 'true';
        } else {
            $answertype = null;
            echo 'false';
        }

        return $answertype;
    } else {
        return;
    }
}
trizz
  • 1,447
  • 12
  • 27
1

Problem is that your variable $sectionConfig is not in the scope of your function. You could use global to get it in the function scope or pass it as a variable:

function addQuestionClass($question) {
    global $sectionConfig; // This gets your variable in the right scope.
    if(is_array($question)) {
        $order = 'order-'.$question['order'];
        $id = ' question-'.$question['id'];

        if(in_array($question['answer'], $sectionConfig['types'])) {
            $answertype = ' type-'.$question['answer'];
            echo 'true';
        } else {
            $answertype = null;
            echo 'false';
        }

        return $answertype;
    } else {
        return;
    }
}
putvande
  • 15,068
  • 3
  • 34
  • 50
  • You better not use `global`, see [this answer](http://stackoverflow.com/a/12446305/590877) for more information. Instead, just pass the array as in my answer. – trizz Feb 12 '14 at 09:27