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?

有帮助吗?

解决方案

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

It should be:

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

其他提示

Don't use the results of print_r as an associative index. You can just use $i:

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

Also, as pointed out by Marty, the increment code should appear as the third expression in your for loop (you have it as the second, so the loop will run infinitely).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top