PHP foreach循环添加额外的循环以及如何从内部返回新数组

时间:2014-12-16 18:10:12

标签: php arrays loops multidimensional-array foreach

我有一个多维的php数组,用于存储我店铺的开始和结束时间。该数组包含有关商店的其他数据,但在此特定功能中,我正在尝试创建一个新数组,其中只有一天作为索引值,而打开和关闭时间作为数组。如下所示

$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' => '', 
);

我当前使用for循环的foreach遍历我现有的数组并返回每天的开放和关闭时间的变量。但是我在底部只包含一个简单的打印件用于调试目的。每次foreach返回正确的一天:打开关闭但它总是返回一个额外的:就好像它正在做一个额外的循环。我已经转储了原始数组,并且每次返回一个数组(7)作为计数。其次,从foreach for循环中返回如上所示的数组的最有效方法是什么。

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>";
    }
}

这是$ this的var_dump - &gt;条目

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) 
}

和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 )

3 个答案:

答案 0 :(得分:2)

从那个可怕的阵列中只拿走了几个:

$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);

打印以下内容:

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
        )

)

答案 1 :(得分:0)

就像我想的那样

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

问题是给你一个额外的行,如果你有几天只是通过数组

if(count($entry) == 7)

以下是您修改的代码:

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>";
    }
  }

答案 2 :(得分:0)

这样的东西?如果我理解你的转储正确......

$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);