0

I don't have a glue how to solve my problem. I want to produce an array like this one:

$days = array( 
            02=>array(NULL,'request_day'), 
            03=>array(NULL,'request_day'), 
            04=>array(NULL,'request_day'), 
            05=>array(NULL,'request_day'), 
        );

I need this to display the requested days on a calendar. Now I deal with a problem that I'm not able to generate dynamic keys (the the keys 02/03/04/05 as stated above).

My code looks like that:

    $days = array();
    $array_framework = array(NULL,'request_day');
    foreach ( $period as $dt )
    {
        echo $dt->format("d");
        $blub = $dt->format("d");
        $days[] = array($blub=>$array_framework);
    }

$period is an array which represent the days between two dates, and $blub prints out one day after another.

3
  • 2
    Have you tried array_push ? Commented May 11, 2013 at 21:22
  • Daniel is right, use array_push, right now you made 3-dimensional array. Commented May 11, 2013 at 21:32
  • can I define my key with array_push?! Commented May 11, 2013 at 21:33

2 Answers 2

1

Remove $days[] = array($blub=>$array_framework); and use:

$days[$blub] = $array_framework;
Sign up to request clarification or add additional context in comments.

2 Comments

thanks but that don't really works. it produces Array ( [04] => Array ( [0] => Array ( [0] => [1] => request_day ) ) [05] => Array ( [0] => Array ( [0] => [1] => request_day ) ) [06] => Array ( [0] => Array ( [0] => [1] => request_day ) ) [07] => Array ( [0] => Array ( [0] => [1] => request_day ) ) )
ok thanks. that array is now what it should be but the result is not as expected... but it's not the array's fault
0

I think that this is what you want to do. If you have problems with the array keys being turned from strings (e.g. 03) into integers (e.g. 3) then cast the array key to a string.

$days = array();
$array_framework = array(NULL,'request_day');
foreach ( $period as $dt )
{
    $days[$dt->format("d")] = $array_framework;
}

4 Comments

thanks but that don't really works. it produces Array ( [04] => Array ( [0] => [1] => request_day ) [05] => Array ( [0] => [1] => request_day ) [06] => Array ( [0] => [1] => request_day ) [07] => Array ( [0] => [1] => request_day ) )
That's what you wanted. You can ignore the 0 and 1 keys in the sub-arrays.
To verify that add a print_r($array_framework) in the line after you declare it.
ok thanks. that array is now what it should be but the result is not as expected... but it's not the array's fault

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.