Search code examples
phpimplode

Implode an array to dayname


I am imploding an array in php like this implode(', ',$array) that will return 7, 3, 4 (number of dayname)

How can I change that to Sunday, Wednesday, Thursday


Solution

  • Simply initialize an $array like this and it will be easy to get name format of the day,

    <?php
    $days = [
      1 => 'Sunday',
      2 => 'Monday',
      3 => 'Tuesday',
      4 => 'Wednesday',
      5 => 'Thursday',
      6 => 'Friday',
      7 => 'Saturday'
    ];
    $array = [7,3,4];
    $result = array_map(function ($a)  use($days) { return $days[$a]; }, $array);
    echo implode(',', $result);
    ?>
    

    DEMO: https://3v4l.org/9Zb5M