Search code examples
phparraysstringkeystring-conversion

Variable and string as array key


I have a problem concerning array keys. I'm trying to result the following array:

$options = array(
    'number 3' => 'number 3',
    'number 6' => 'number 6',
    'number 9' => 'number 9',
    'number 12' => 'number 12'
);

I'm using the following function:

function number_count() {

    $array = array();

    for( $i = 3 ; $i+3 ; $i <= 12 ) {
        $string_i = print_r($i, true);
        $array[$string_i . 'px'] = $string_i . 'px';
    }

    return $array;
}

    $options= number_count();

I know there is some serious error that I can't understand because the page is blocking when I try to execute the code. How I can insert a variable and key, and variable and value in the array?


Solution

  • There's actually an error in your for-loop...

    It should be:

    for ($i = 3;$i <= 12; $i = $i + 3) {