user1881482
user1881482

Reputation: 746

PHP foreach loop adding extra loop and how to return new array from within

I have a multidimensional php array that stores the opening and closing times of my shop. The array contains other data about the shop to however in this particular function I am trying to create a new array with only the day as an index value and the open and close hours as an array. Like the following

$hours = array(
    'mon' => array('11:00-23:00'), 
    'tue' => array('11:00-23:00'),
    'wed' => array('11:00-22:30'),
    'thu' => array('11:00-02:30'),
    'fri' => array('11:00-02:30'),
    'sat' => array('11:00-02:30'),
    'sun' => '', 
);

My current foreach with a for loop goes through my existing array and returns a variable for each day, open and close time. However I have included just a simple print at the bottom for debugging purposes. Everytime the foreach returns the correct day: open to close however it always returns one extra :to as if it's doing an extra loop. I have dumped the original array and every time it returns an array with (7) as the count. Secondly what is the most efficient way to return an array like the one above from within this foreach for loop.

foreach ($this -> entries as $entry) {
    for ($i = 0; $i < count($entry); $i++) {
        $day = $entry[$i]['day_open']['value'];
        $open = $entry[$i]['open_hour']['value'];
        $close = $entry[$i]['close_hour']['value'];
        print $day . " : " . $open . " to " . $close . "<br>";
    }
}

Here is the var_dump of $this -> entries

array(3) { ["entries"]=> array(7) { 
    [0]=> array(11) { ["id"]=> string(1) "1" ["created"]=> int(1418692508) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "1" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Sun" ["val"]=> string(3) "Sun" ["value"]=> string(3) "Sun" } ["last"]=> string(1) "0" ["odd_even"]=> string(3) "odd" ["count"]=> int(1) } 
    [1]=> array(11) { ["id"]=> string(1) "2" ["created"]=> int(1418692519) ["updated"]=> int(1418698391) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "2" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Mon" ["val"]=> string(3) "Mon" ["value"]=> string(3) "Mon" } ["last"]=> string(1) "0" ["odd_even"]=> string(4) "even" ["count"]=> int(2) } 
    [2]=> array(11) { ["id"]=> string(1) "3" ["created"]=> int(1418692525) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "3" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Tue" ["val"]=> string(3) "Tue" ["value"]=> string(3) "Tue" } ["last"]=> string(1) "0" ["odd_even"]=> string(3) "odd" ["count"]=> int(3) } 
    [3]=> array(11) { ["id"]=> string(1) "4" ["created"]=> int(1418692529) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "4" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Wed" ["val"]=> string(3) "Wed" ["value"]=> string(3) "Wed" } ["last"]=> string(1) "0" ["odd_even"]=> string(4) "even" ["count"]=> int(4) } 
    [4]=> array(11) { ["id"]=> string(1) "5" ["created"]=> int(1418692536) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "5" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Thu" ["val"]=> string(3) "Thu" ["value"]=> string(3) "Thu" } ["last"]=> string(1) "0" ["odd_even"]=> string(3) "odd" ["count"]=> int(5) } 
    [5]=> array(11) { ["id"]=> string(1) "6" ["created"]=> int(1418692549) ["updated"]=> int(1418698491) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "6" ["close_hour"]=> array(3) { ["key"]=> string(4) "12am" ["val"]=> string(8) "12:00 AM" ["value"]=> string(8) "12:00 AM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Fri" ["val"]=> string(3) "Fri" ["value"]=> string(3) "Fri" } ["last"]=> string(1) "0" ["odd_even"]=> string(4) "even" ["count"]=> int(6) } 
    [6]=> array(11) { ["id"]=> string(1) "7" ["created"]=> int(1418692559) ["updated"]=> int(1418698459) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "7" ["close_hour"]=> array(3) { ["key"]=> string(4) "12am" ["val"]=> string(8) "12:00 AM" ["value"]=> string(8) "12:00 AM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Sat" ["val"]=> string(3) "Sat" ["value"]=> string(3) "Sat" } ["last"]=> string(1) "1" ["odd_even"]=> string(3) "odd" ["count"]=> int(7) } 
    } 
    ["pagination"]=> NULL ["total"]=> int(7) 
}

and in print_r

Array ( [entries] => Array (
[0] => Array ( [id] => 1 [created] => 1418692508 [updated] => [created_by] => 1 [ordering_count] => 1 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Sun [val] => Sun [value] => Sun ) [last] => 0 [odd_even] => odd [count] => 1 ) 
[1] => Array ( [id] => 2 [created] => 1418692519 [updated] => 1418698391 [created_by] => 1 [ordering_count] => 2 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Mon [val] => Mon [value] => Mon ) [last] => 0 [odd_even] => even [count] => 2 ) 
[2] => Array ( [id] => 3 [created] => 1418692525 [updated] => [created_by] => 1 [ordering_count] => 3 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Tue [val] => Tue [value] => Tue ) [last] => 0 [odd_even] => odd [count] => 3 ) 
[3] => Array ( [id] => 4 [created] => 1418692529 [updated] => [created_by] => 1 [ordering_count] => 4 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Wed [val] => Wed [value] => Wed ) [last] => 0 [odd_even] => even [count] => 4 ) 
[4] => Array ( [id] => 5 [created] => 1418692536 [updated] => [created_by] => 1 [ordering_count] => 5 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Thu [val] => Thu [value] => Thu ) [last] => 0 [odd_even] => odd [count] => 5 ) 
[5] => Array ( [id] => 6 [created] => 1418692549 [updated] => 1418698491 [created_by] => 1 [ordering_count] => 6 [close_hour] => Array ( [key] => 12am [val] => 12:00 AM [value] => 12:00 AM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Fri [val] => Fri [value] => Fri ) [last] => 0 [odd_even] => even [count] => 6 ) 
[6] => Array ( [id] => 7 [created] => 1418692559 [updated] => 1418698459 [created_by] => 1 [ordering_count] => 7 [close_hour] => Array ( [key] => 12am [val] => 12:00 AM [value] => 12:00 AM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Sat [val] => Sat [value] => Sat ) [last] => 1 [odd_even] => odd [count] => 7 )
) 
[pagination] => [total] => 7 )

Upvotes: 1

Views: 2166

Answers (3)

Demodave
Demodave

Reputation: 6632

Like I thought the

["pagination"]=> NULL ["total"]=> int(7)

was the problem giving you an extra row, simply only go through the array if you have days

if(count($entry) == 7)

Here is your code modified:

foreach ($entries as $entry) {

  if(count($entry) == 7)
  {
    for ($i = 0; $i < count($entry); $i++) {
        $day = $entry[$i]['day_open']['value'];
        $open = $entry[$i]['open_hour']['value'];
        $close = $entry[$i]['close_hour']['value'];
        print $day . " : " . $open . " to " . $close . "<br>";
    }
  }

Upvotes: 0

OIS
OIS

Reputation: 10033

Something like this? If I understood your dump correctly...

$days = [];
foreach ($this-entries["entries"] as $entry) {
    $days[strtolower($entry['day_open']['value'])] = [
        sprintf(
            "%s-%s", 
            substr($entry['open_hour']['value'], 0, 5), 
            substr($entry['close_hour']['value'], 0, 5)
        )
    ];
}
var_dump($days);

Upvotes: 0

Scott-David Jones
Scott-David Jones

Reputation: 412

taking only a few from that ghastly array:

$oldArray = array(
    "entries"=> array(
        array( 
            "id"=>"1", 
            "created"=> 1418692508, 
            "updated"=> false, 
            "created_by"=> "1", 
            "ordering_count"=> "1",
            "close_hour" => array(
                "key"=> "10pm",
                "val"=> "10:00 PM",
                "value"=> "10:00 PM" 
            ),
            "open_hour"=> array(
                "key"=> "2pm",
                "val"=> "02:00 PM",
                "value"=> "02:00 PM"
            ),
            "day_open"=> array(
                "key"=> "Mon",
                "val"=> "Mon",
                "value"=> "Mon"
            ),
            "last"=> "0",
            "odd_even"=> "odd",
            "count"=> 1 
        ), 
        array( 
            "id"=>"1", 
            "created"=> 1418692508, 
            "updated"=> false, 
            "created_by"=> "1", 
            "ordering_count"=> "1",
            "close_hour" => array(
                "key"=> "10pm",
                "val"=> "10:00 PM",
                "value"=> "10:00 PM" 
            ),
            "open_hour"=> array(
                "key"=> "2pm",
                "val"=> "02:00 PM",
                "value"=> "02:00 PM"
            ),
            "day_open"=> array(
                "key"=> "Tues",
                "val"=> "Tues",
                "value"=> "Tues"
            ),
            "last"=> "0",
            "odd_even"=> "odd",
            "count"=> 1 
        ), 
        array( 
            "id"=>"1", 
            "created"=> 1418692508, 
            "updated"=> false, 
            "created_by"=> "1", 
            "ordering_count"=> "1",
            "close_hour" => array(
                "key"=> "10pm",
                "val"=> "10:00 PM",
                "value"=> "10:00 PM" 
            ),
            "open_hour"=> array(
                "key"=> "2pm",
                "val"=> "02:00 PM",
                "value"=> "02:00 PM"
            ),
            "day_open"=> array(
                "key"=> "Weds",
                "val"=> "Weds",
                "value"=> "Weds"
            ),
            "last"=> "0",
            "odd_even"=> "odd",
            "count"=> 1 
        ),  
    ),
    "pagination"=> NULL,
    "total"=> 7  
);
$hours = array();
foreach ($oldArray['entries'] as $e) {
        $h = '';
        if (isset($e['open_hour']['value'])) {
            $h = $e['open_hour']['value'].'-'.$e['close_hour']['value'];
        }

        $hours[$e['day_open']['value']] = array(
            $h,
        );
}
echo "<pre>";print_r($hours);

prints the following:

Array
(
    [Mon] => Array
        (
            [0] => 02:00 PM-10:00 PM
        )

    [Tues] => Array
        (
            [0] => 02:00 PM-10:00 PM
        )

    [Weds] => Array
        (
            [0] => 02:00 PM-10:00 PM
        )

)

Upvotes: 2

Related Questions