在函数中传递foreach循环

时间:2014-08-06 13:48:31

标签: php foreach

只是想知道我们是否可以采取以下措施:

我想在函数中传递foreach循环作为属性。

<?php

TestMethodForEach( foreach($_POST['activity'] as $activityID){ $activityID; } );

?>

如何在php中实现此功能?

这是我的真实代码:

<?php

 $accountServices->RL_ChangeInActivities(
                            new IhcStructRL_ChangeInActivities(
                                new IhcStructChangeActivity(
                                '00000000-0000-0000-0000-000000000000',
                                $accountID,
                                $_POST['serviceID'],
                                '1',
                                'IhcStructChangeActivity',
                                'teset,etsagad, asdfasd',
                                    new IhcStructArrayOfAccountActivity(
                                            new IhcStructAccountActivity(
                                                '00000000-0000-0000-0000-000000000000',
                                                'activityID 1',
                                                $accountID,
                                                'IhcStructAccountActivity',
                                                'ActivityNumber'
                                                )
                                        )
                                )
                        )
                    );

  ?>

其实我想重复这个对象IhcStructArrayOfAccountActivity。

您将在此对象中看到(ActivityID 1)(新的IhcStructAccountActivity),因此我有超过5个活动ID或者可能更多,无所谓。那么我怎么能动态地重复这个对象,所以这应该像

 <?php

new IhcStructArrayOfAccountActivity(
new IhcStructAccountActivity(
'00000000-0000-0000-0000-000000000000',
'activityID 1',
$accountID,
'IhcStructAccountActivity',
'ActivityNumber'
)
)

 new IhcStructArrayOfAccountActivity(
new IhcStructAccountActivity(
'00000000-0000-0000-0000-000000000000',
'activityID 2',
$accountID,
'IhcStructAccountActivity',
'ActivityNumber'
)
)

 new IhcStructArrayOfAccountActivity(
new IhcStructAccountActivity(
'00000000-0000-0000-0000-000000000000',
'activityID 3',
$accountID,
'IhcStructAccountActivity',
'ActivityNumber'
)
)

 ?>

希望你现在明白。

2 个答案:

答案 0 :(得分:0)

<?php
    foreach ($_POST['acitivity'] as $activityID) {
        TestMethodForEach($activityID);
    }
?>

或者array_walk会做的。

答案 1 :(得分:0)

正确的代码是:

<?php  foreach($_POST['activity'] as $activityID) {
        TestMethodForEach($activityID;);
       }
 ?>

我希望这就是你正在寻找的东西

相关问题