我如何在PHP中求和数组元素?

时间:2015-09-09 09:38:33

标签: php arrays foreach

我需要这个结果:

breakfast: Monday, Tuesday, Thursday

lunch: Monday, Saturday

dinner: Tuesday, Sunday

从这个阵列:

Array
(
[breakfast] => Array
    (
        [0] => Monday
        [1] => Tuesday
        [2] => Thursday
    )

[lunch] => Array
    (
        [0] => Monday
        [1] => Saturday
    )

[dinner] => Array
    (
        [0] => Tuesday
        [1] => Sunday
    )
)

我该怎么办?

2 个答案:

答案 0 :(得分:1)

试试这个,请注意数组初始化的语法错误

$a = Array
(
    [breakfast] => Array
    (
        [0] => Monday
        [1] => Tuesday
        [2] => Thursday
    )

    [lunch] => Array
    (
        [0] => Monday
        [1] => Saturday
    )

    [dinner] => Array
    (
        [0] => Tuesday
        [1] => Sunday
    )
);    


 foreach($a as $key => $val){
        echo $key . " : " . implode(",",$val)."<br>";
    }

答案 1 :(得分:0)

试试这个:

foreach($array as $key => $value){
      echo $key . ' : ' . implode(',',$value) .'<br />';
}

http://php.net/manual/en/function.implode.php