用于从对象

时间:2017-01-08 03:29:53

标签: php arrays function recursion multidimensional-array

我目前正在尝试编写一个返回结构化为JSON的数据的PHP脚本。下面你可以看到我想要实现的输出的一个例子:

Array
(
    [0] => GameObject
        (
            [id] => 1
            [parent_id] => 0
            [title] => Parent Page
            [Rounds] => Array
                        (
                           [0] => RoundObject
                                (
                                    [id] => 2
                                    [parent_id] => 1
                                    [title] => Sub Page
                                    [actions] => Array
                                )
                            [1] => RoundObject
                                (
                                    [id] => 2
                                    [parent_id] => 1
                                    [title] => Sub Page
                                    [actions] => Array
                                                (
                                                    [0] => ActionObject
                                                        (
                                                            [id] => 3
                                                            [parent_id] => 1
                                                            [title] => Sub Sub Page
                                                        )
                                                    [0] => ActionObject
                                                        (
                                                            [id] => 3
                                                            [parent_id] => 1
                                                            [title] => Sub                           Sub Page
                                                        )
                                                )
                                )
                        )
        )
    [1] => GameObject
        (
            [id] => 4
            [parent_id] => 0
            [title] => Another Parent Page
        )
)

递归函数假设从GameObject或GameObjects数组开始。然后在GameObjects上调用'getChildren()'并将结果放在GameObject ['Rounds']中。然后使用RoundObject执行相同的操作:调用RoundObject的'getChildren()'并将结果放在RoundObject ['actions']中。

像GameObject这样的对象有多个孩子,孩子> 0.我知道递归函数是如何工作的,但我不知道如何构造数据以匹配我的输出示例。

任何帮助都是受到赞赏的!经过几个小时尝试不同的解决方案后,我绝望了。)。

需要澄清吗?发表评论:D

修改 我的代码目前如下所示:

$response = create_response_object($game, $entityManager);

function create_response_object($arg, $entityManager)
{
    if(is_object($arg))
    {
        if($arg instanceof IWithChildren)
        {
            //If it's an object and it implements IWithChildren -> get its children
            $arg->children = $arg->getChildren($entityManager);
        }
        else
        {
            //End of the line. $arg is an object but doesn't have any children
        }
    }

    //$arg is an array filled with objects
    if(is_array($arg))
    {
        //If it's an array, loop through its contents
        foreach ($arg as $a)
        {
            //$arg['child'] = create_response_object($a, $entityManager);
            return create_response_object($a, $entityManager);
        }
    }

    return $arg;
}

print_r($response);

这是函数返回的内容:

Game Object
(
    [id:Game:private] => 9
    [gamestate:Game:private] => 0
    [player1ID:Game:private] => 30
    [player2ID:Game:private] => 1
    [currentPlayer:Game:private] => 1
    [winnerID:Game:private] => 
    [errorCollector:Game:private] => 
    [entityManager:Game:private] => 
    [children] => Array
        (
            [0] => Round Object
                (
                    [id:Round:private] => 7
                    [winnerID:Round:private] => 
                    [gameID:Round:private] => 9
                    [roundNumber:Round:private] => 0
                )

            [1] => Round Object
                (
                    [id:Round:private] => 8
                    [winnerID:Round:private] => 
                    [gameID:Round:private] => 9
                    [roundNumber:Round:private] => 1
                )

        )

)

预期回应: 当它添加一个名为children的属性并且值为一个带有所述对象的子元素的数组时,上面的响应似乎开始很好。但它永远不会超越RoundObject,我希望该函数继续检索子项。我认为它与我的函数中的递归有关...

1 个答案:

答案 0 :(得分:1)

  1. 迭代数组不应该从函数返回,只是将子节点推入每个对象。我不确定你的代码是如何交互的,但也许你会通过这个例子得到这个想法:

    foreach ($arg as $a) {
        //since $a is an object pointer ther's no need to reasign (using reference)
        //$a will have its children after calling this function
        create_response_object($a, $entityManager);
    }
    
  2. 看起来像对象要求经理找到自己的孩子(!?)我不知道从哪里开始......没关系
  3. 当“聚集”孩子的时候,每个孩子都应该被要求生孩子,所以你需要这样的东西(递归地):

    //go to function as an array & iterate populating each child with its children
    $arg->children = create_response_object($arg->getChildren($entityManager), $entityManager);
    
  4. 您需要另一个if / else来确定子属性Rounds / Actions
  5. 问题是以程序方式递归,而不是从对象的角度来看。重新思考你的设计。请。
  6. 提示:构建游戏对象的聚合(GameState ..或其他东西)。当被要求Response时,这个聚合将迭代并从每个游戏对象收集响应数据,Game obejct要求其数据需要遍历其子代......等等。