【问题标题】:Pass foreach loop in function在函数中传递foreach循环
【发布时间】:2014-09-29 11:45:28
【问题描述】:

只是想知道我们是否可以执行以下操作:

我想将函数中的 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。

您将在此对象(新的 IhcStructAccountActivity)中看到(ActivityID 1),所以我有超过 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'
)
)

 ?>

希望你现在明白了。

【问题讨论】:

  • 您为什么要这样做?您要寻找的行为是什么?
  • 您已经编写了代码,为什么不直接运行它并回答您自己的问题?
  • 是的兄弟,我相信这没有任何意义,但有没有这样的方法?实际上我想在 xml 中重复该值。
  • 做旧的 Reverse-a-roo 以获得相同的结果
  • 我已经更新了代码,请看一下。

标签: php foreach


【解决方案1】:

正确的代码是:

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

我希望这就是你要找的东西

【讨论】:

    【解决方案2】:
    <?php
        foreach ($_POST['acitivity'] as $activityID) {
            TestMethodForEach($activityID);
        }
    ?>
    

    或者array_walk 会做这件事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 2020-06-19
      • 1970-01-01
      • 2017-07-12
      相关资源
      最近更新 更多