Jump to content

Display Multidiamentional array in a customize way.


thara

Recommended Posts

I have a multidimensional array and it is like this -

    Array
    (
        [Monday] => Array
            (
                [open] => 05.00 PM
                [close] => 04.00 PM
                [state] => 0
            )
    
        [Tuesday] => Array
            (
                [open] =>
                [close] =>
                [state] => 1
            )
    
        [Wednesday] => Array
            (
                [open] => 03.00 AM
                [close] => 06.00 PM
                [state] => 0
            )
    
        [Thursday] => Array
            (
                [open] =>
                [close] =>
                [state] => 1
            )
    
        [Friday] => Array
            (
                [open] => 05.00 PM
                [close] => 03.00 PM
                [state] => 0
            )
    
        [Saturday] => Array
            (
                [open] => 05.00 PM
                [close] => 06.00 PM
                [state] => 0
            )
    
        [Sunday] => Array
            (
                [open] =>
                [close] =>
                [state] => 1
            )
    
    )

Using this array I want to make an output like this -
 

    Monday          - 05.00 PM - 04.00 PM
    Tuesday      - Closed
    Wednesday    - 03.00 AM - 06.00 PM
    Thursday     - Closed
    Friday          - 05.00 PM - 03.00 PM
    Saturday     - 05.00 PM - 06.00 PM
    Sunday          - Closed

I tried it with 2 foreach loop. But I couldn't get to work to expecting output.
 

    foreach ($result as $days => $values) {
        echo "$days";
        foreach ($values as $k) {
            echo " - $k";
        }
        echo "<br/>";
    }

Its ouptput is similar to this -
 

    Monday - 05.00 PM - 04.00 PM - 0
    Tuesday - - - 1
    Wednesday - 03.00 AM - 06.00 PM - 0
    Thursday - - - 1
    Friday - 05.00 PM - 03.00 PM - 0
    Saturday - 05.00 PM - 06.00 PM - 0
    Sunday - - - 1

Can anybody tell me how can I figure this out?

NOTE: if `state = 0` it doesn't need to display and `state = 1` it should be `Closed`

Any ideas would be greatly appreciated.
Thank you.

echo '<pre>';
foreach($result as $day => $store_data) {
  if ( !empty($store_data['open']) ) {
    echo "{$day}\t - {$store_data['open']} - {$store_data['close']}";
  } else {
    echo "{$day}\t - Closed";
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.