Home > Blockchain >  Replace PHP array value with current day
Replace PHP array value with current day

Time:01-14

I have this array:

$flights = Array (
   [0] => Array (
      [day] => 0
      [flights] => 0
   )
   [1] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [2] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [3] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [4] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [5] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [6] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [7] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
   [8] => Array ( 
      [day] => 0 
      [flights] => 0 
   ) 
   [9] => Array ( 
      [day] => 0 
      [flights] => 0 
   ) 
   [10] => Array ( 
      [day] => 0 
      [flights] => 0 
   ) 
   [11] => Array ( 
      [day] => 0 
      [flights] => 0 
   ) 
   [12] => Array ( 
      [day] => 0 
      [flights] => 0 
   )
)

And I want to replace all [day] values with the current correspondent day, like:

Array (
   [0] => Array (
      [day] => 01
      [flights] => 0
   )
   [1] => Array ( 
      [day] => 02 
      [flights] => 0 
   )
   [2] => Array ( 
      [day] => 03
      [flights] => 0
   )
...

The code is creating the array key numbers according to the current day of the month, so the array has the correct number of keys for today (13/01).

What is the function to do this?

CodePudding user response:

foreach($flights as $k=>$f){
  $flights[$k]['day'] =   $k;
}

CodePudding user response:

After some deeper search I managed to solve it with this:

$day_counter = 1;
foreach ($flights as $key => $value)
{
  $flights[$key]['day'] = $day_counter;
  $day_counter  ;
}

So I start with day "1" as $day_counter and increment after replacing it in array.

  •  Tags:  
  • Related