Domanda

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?

È stato utile?

Soluzione

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

It should be:

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top