我想收集每个membership_name值,并显示它。我尝试了一些foreach循环,但是它们总是将我的drupal安装警告为无效的foreach()命令。
这是print_r($ myMemberships);
Array
(
[1] => Array
(
[3] => Array
(
[id] => 3
[membership_id] => 3
[contact_id] => 1
[membership_contact_id] => 1
[membership_type_id] => 1
[join_date] => 2011-08-23
[start_date] => 2011-08-23
[membership_start_date] => 2011-08-23
[status_id] => 2
[is_override] => 1
[is_test] => 0
[member_is_test] => 0
[is_pay_later] => 0
[member_is_pay_later] => 0
[membership_name] => Orientation
[relationship_name] => Volunteer for
)
[4] => Array
(
[id] => 4
[membership_id] => 4
[contact_id] => 1
[membership_contact_id] => 1
[membership_type_id] => 2
[join_date] => 2011-08-23
[start_date] => 2011-08-23
[membership_start_date] => 2011-08-23
[status_id] => 2
[is_override] => 1
[is_test] => 0
[member_is_test] => 0
[is_pay_later] => 0
[member_is_pay_later] => 0
[membership_name] => Field Production
[relationship_name] => Volunteer for
)
[5] => Array
(
[id] => 5
[membership_id] => 5
[contact_id] => 1
[membership_contact_id] => 1
[membership_type_id] => 3
[join_date] => 2011-08-23
[start_date] => 2011-08-23
[membership_start_date] => 2011-08-23
[status_id] => 2
[is_override] => 1
[is_test] => 0
[member_is_test] => 0
[is_pay_later] => 0
[member_is_pay_later] => 0
[membership_name] => Graphic Design
[relationship_name] => Volunteer for
)
)
[record_count] => 7
)
此外,我想将每个membership_name确保为变量,以便向用户授予如下权限:
<?php
if ($orientation) {
// execute code
} else {
// otherwise execute
}
<?php
if ($fieldproduction) {
// execute code
} else {
// otherwise execute
}
// etc... etc...
这就是我用它来调用它,可能是完全错误的:
foreach ( $myMemberships as $myMembership ) {
foreach ( $myMembership as $value => $final ) {
echo 'hello';
}
}
答案 0 :(得分:2)
这是导致问题的数组底部的record_count
项。您的外部循环将为7
分配内部循环$myMemberShip
。然后你尝试对7
做一个foreach - 因为它不是一个数组,你得到警告。
答案 1 :(得分:1)
如果那是您的数组,那么您需要在myMemberships[1]
中查找成员,因此您的代码应为
foreach ($myMemberships[1] as $myMembership ) {
foreach ( $myMembership as $value => $final ) {
// here you have membership properties
}
}
如果你的数组是动态的,你必须检查它,所以你应该做这样的事情
foreach ($myMemberships[1] as $myMembership ) {
if (is_array($myMembership)) {
foreach ( $myMembership as $value => $final ) {
// here you have membership properties
}
}
}