我已经尝试了一段时间,但我似乎无法遍历多维数组。
我有这个数组:
$Work["work_time"] = array();
$Work["break_time"] = array();
$Work["meeting_time"] = array();
$Work["login_time"] = array();
$Work["logout_time"] = array();
$Work["work_date"] = array();
哪个print_r($ Work)输出此
Array
(
[work_time] => Array
(
[0] => 0.00
[1] => 3.96
[2] => 7.75
)
[break_time] => Array
(
[0] => 0.00
[1] => 0.00
[2] => 1.06
)
[meeting_time] => Array
(
[0] => 0.00
[1] => 0.00
[2] => 0.00
)
[login_time] => Array
(
[0] => 10:11
[1] => 08:48
[2] => 09:09
)
[logout_time] => Array
(
[0] => 00:00
[1] => 13:00
[2] => 17:59
)
[work_date] => Array
(
[0] => 2018-04-13
[1] => 2018-04-16
[2] => 2018-04-17
)
)
然后我尝试使用foreach循环来遍历它并获取值,但它什么都不返回..
foreach ($Work as $row) {
echo $row["login_time"];
}
我错过了什么?
答案 0 :(得分:3)
If you really dont know how many dimensions you are dealing with, a recursive solution is what you need. Here is my code:
$Work["work_time"] = array(1, 2, 3);
$Work["break_time"] = array(4, 5, 6);
$Work["meeting_time"] = array(7, 8, 9);
$Work["login_time"] = array(10, 11, 12);
$Work["logout_time"] = array(13, 14, 15);
$Work["work_date"] = array(16, 17, 18);
function forLoop($array){
foreach ($array as $row) {
if(is_array($row)){
forLoop($row);
}
else{
echo $row;
echo " ";
}
}
}
forLoop($Work);
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
You actually need to make a formating or something to make it work the way you want it to. Edit: forLoop function calls it's self so it will work on any dimension array. Tell me what you think!
答案 1 :(得分:3)
您尝试使用foreach循环迭代的数组与您拥有的数组不同。为了使用该循环,您的数组将需要像这样:
[
[
'work_time' => 0.00,
'break_time' => 0.00,
'meeting_time' => 0.00,
'login_time' => '10:11',
'logout_time' => '00:00',
'work_date' => '2018-04-13'
],
[
'work_time' => 3.96,
'break_time' => 0.00,
'meeting_time' => 0.00,
'login_time' => '08:48',
'logout_time' => '13:00',
'work_date' => '2018-04-16'
],
[
'work_time' => 7.75,
'break_time' => 1.06,
'meeting_time' => 0.00,
'login_time' => '09:09',
'logout_time' => '17:59',
'work_date' => '2018-04-17'
],
];
如果你有一个包含多行输入的HTML表单,那么你拥有的数组看起来就像输入的类型一样:
<input type="text" name="work[work_time][]">
如果是这种情况,您可以通过切换键并为每行指定数字键来获得类似于上面显示的数组:
<input type="text" name="work[0][work_time]">
然后,将数据作为行访问会更有意义。如果情况并非如此,那么,没关系。 :)
另一方面,使用您拥有的数组,您可以直接迭代其中一个内部数组。例如,如果您想显示login_time值,那么它只是
foreach ($Work['login_time'] as $login_time) {
echo $login_time;
}
如果您需要从其他内部数组中获取相应的值,您可以使用您正在迭代的数组中的键来访问它们:
foreach ($Work['login_time'] as $key => $login_time) {
echo 'login time: ' . $login_time . ', work_date: ' . $Work['work_date'][$key];
}
答案 2 :(得分:1)
If you already know the key you need (login_time), you do not need to iterate the array. You can access it directly as $Work["login_time"]
.
If you want to iterate $Work["login_time"]
, you can use foreach
:
foreach ($Work['login_time'] as $row) {
echo $row;
}
答案 3 :(得分:0)
Probably need multiple for loops
foreach ($Work as $row) {
foreach($row as $key => $rs) {
echo $rs;
}
}
答案 4 :(得分:0)
To loop completely you would need a 2nd foreach inside the first foreach. Like This:
foreach ($Work as $row) {
foreach ($row as $key => $value) {
echo "$key - $value";
}
}
答案 5 :(得分:0)
foreach ($dataArray as $innerArray){
foreach ($innerArray as $key=>$value){
// here is where you access the array -- $value
}
}