Jump to content

php array


subhomoy
Go to solution Solved by ginerjm,

Recommended Posts

Hello everybody

 

I am facing some problem with array..

 

I have a na array like this

$arr = array(

  [0]=>1,

[1]=>10,

[2]=>166,

[7]=>12,

[15]=>1365,

[20]=>1253,

.

.

.

.

.

[31]=>40

);

What i want is that to show the whole array.

 

The keys consist of all the dates of a month..

 

If no specific dates exits then it will show 0 for that corresponding dates..

 

Like the above array does not contain keys [3]. [4], [5], ....

 

I awant to show them as zero...

 

Like this

$arr = array(

  [0]=>1,

[1]=>10,

[2]=>166,

[3]=>0,

[4]=>0,

[5]=>0,

[6]=>0,

[7]=>12,

[15]=>1365,

[20]=>1253,

.

.

.

.

.

[31]=>40

);

Any help will be greatly appreciated... Thank u in advance....

 

Link to comment
Share on other sites

Cronix - Not familiar with array_fill_keys() nor range() but after reading the manual I think there is a flaw in this.  Your range will produce a set of values ending with whatever is the highest EXISTING key in the array.  Then array_fill_keys assigns a 0 value to all of the indices in the range set.  What about the days that were not in the original array higher than the existing max day?

Link to comment
Share on other sites

Yeah, I was just going by the example he posted. If he needs that he can just change max(array_keys($arr)) to the number of days in the given month. He also has a "0" day, not sure how that's possible if it's the days of the month.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.