I'd like to combine some arrays with others to create a new array, but it doesn't work.

Asked 2 months ago, Updated 2 months ago, 3 views

Array of specific days of the month (November) and
There is an array of fruits to assign by day of the week.
I'd like to use this to insert a specific array into the appropriate day of the week, but for some reason it stops at 23...
I don't know why.

(We will also implement a restricted implementation in the next week, every second week.)


$fruitArray=array(
            'tue' = > 'banana',
            'fri' = > 'apple',
            'sat' = > 'orange',
          );



$dayArray=
 array(
  1 = > 'tue',
  2 = > 'wed',
  4 = > 'fri',
  5 = > 'sat',
  7 = > 'mon',
  8 = > 'tue',
  9 = > 'wed',
  10 = > 'thu',
  11 = > 'fri',
  12 = > 'sat',
  14 = > 'mon',
  15 = > 'tue',
  16 = > 'wed',
  17=>'thu',
  18=>'fri',
  19 = > 'sat',
  21 = > 'mon',
  22 = > 'tue',
  24 = > 'thu',
  25 = > 'fri',
  26 = > 'sat',
  28 = > 'mon',
  29 = > 'tue',
  30 = > 'wed',
) 
foreach($dayArray as$day){
          if(isset($fruitArray[$day])){
            $result[] = [$day, $fruitArray[$day]];
          } else{
            $result[] = [$day, null];
          }
        }

The result stops at 23 for some reason.

array(
  0 = > 
  array(
    0 = > 'tue',
    1 = > 'banana',
  ),
  1 = > 
  array(
    0 = > 'wed',
    1 = >',
  ),
  2 = > 
  array(
    0 = > 'fri',
    1=>'apple',
  ),
  3=> 
  array(
    0 = > 'sat',
    1 = > 'orange',
  ),
  4 = > 
  array(
    0 = > 'mon',
    1 = >',
  ),
  5=> 
  array(
    0 = > 'tue',
    1 = > 'banana',
  ),
  6 = > 
  array(
    0 = > 'wed',
    1 = >',
  ),
  7 = > 
  array(
    0 = > 'thu',
    1 = >',
  ),
  8=> 
  array(
    0 = > 'fri',
    1=>'apple',
  ),
  9=> 
  array(
    0 = > 'sat',
    1 = > 'orange',
  ),
  10 = > 
  array(
    0 = > 'mon',
    1 = >',
  ),
  11 = > 
  array(
    0 = > 'tue',
    1 = > 'banana',
  ),
  12 = > 
  array(
    0 = > 'wed',
    1 = >',
  ),
  13=> 
  array(
    0 = > 'thu',
    1 = >',
  ),
  14 = > 
  array(
    0 = > 'fri',
    1=>'apple',
  ),
  15 = > 
  array(
    0 = > 'sat',
    1 = > 'orange',
  ),
  16 = > 
  array(
    0 = > 'mon',
    1 = >',
  ),
  17=> 
  array(
    0 = > 'tue',
    1 = > 'banana',
  ),
  18=> 
  array(
    0 = > 'thu',
    1 = >',
  ),
  19=> 
  array(
    0 = > 'fri',
    1=>'apple',
  ),
  20=> 
  array(
    0 = > 'sat',
    1 = > 'orange',
  ),
  21 = > 
  array(
    0 = > 'mon',
    1 = >',
  ),
  22 = > 
  array(
    0 = > 'tue',
    1 = > 'banana',
  ),
  23 = > 
  array(
    0 = > 'wed',
    1 = >',
  ),
)  

php

2022-09-30 12:05

3 Answers

Normal operation.
If no key is specified for the array, consecutive numbers are re-numbered from 0.
$dayArray has only 24 elements registered, so the number is up to 23.
If you want to keep the array key, you can specify the array key as follows:

foreach($dayArray as$key=>$day){
          if(isset($fruitArray[$day])){
            $result[$key]=[$day,$fruitArray[$day]];
          } else{
            $result [$key] = [$day, null];
          }
        }

I hope it will be helpful.


2022-09-30 12:05

For your information, if it's a one-dimensional array of string data only, you can replace it with str_replace.

$dayArray=
  array(
    1 = > 'tue',
    2 = > 'wed',
    4 = > 'fri',
    5 = > 'sat',
    7 = > 'mon',
    8 = > 'tue',
    9 = > 'wed',
    10 = > 'thu',
    11 = > 'fri',
    12 = > 'sat',
    14 = > 'mon',
    15 = > 'tue',
    16 = > 'wed',
    17=>'thu',
    18=>'fri',
    19 = > 'sat',
    21 = > 'mon',
    22 = > 'tue',
    24 = > 'thu',
    25 = > 'fri',
    26 = > 'sat',
    28 = > 'mon',
    29 = > 'tue',
    30 = > 'wed',
  );

$new_array=str_replace(array('tue', 'fri', 'sat'), array('banana', 'apple', 'orange'), $dayArray);
$new_array=str_replace(array('mon', 'wed', 'thu', 'sun'), ', $new_array);
print_r($new_array);


2022-09-30 12:05

$result=array_map(fn($v)=>[$v,$fruitArray[$v]?null],$dayArray);


2022-09-30 12:05

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.