带有嵌套数组的PHP Laravel递归函数 [英] PHP Laravel recursive function with nested array

查看:133
本文介绍了带有嵌套数组的PHP Laravel递归函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Laravel 4MySQL后端一起使用.

I am using Laravel 4 with MySQL back-end.

我想使用recursive functionnested array的记录存储到数据库中.

I want to store records of a nested array into database using recursive function.

输入数组如下:

Array
(
    [0] => Array
    (
        'id' => 561,
        'type' => 'q',
        'subtype' => 'boolean',
        'title' => 'Did you..?',
        'parent' => 560,
        'created_at' => '2014-07-09 09:45:50',
        'updated_at' => NULL,
        'deleted_at' => NULL,
        'children' => Array
            (
                [0] => Array
                    (
                        'id' => 562,
                        'type' => 'o',
                        'subtype' => NULL,
                        'title' => 'Yes',
                        'parent' => 561,
                        'created_at' => '2014-07-09 09:45:50',
                        'updated_at' => NULL,
                        'deleted_at' => NULL,
                        'children' => Array
                            (
                            )
                    )
                [1] => Array
                    (
                        'id' => 563,
                        'type' => 'answer',
                        'subtype' => NULL,
                        'title' => 'No',
                        'parent' => 561,
                        'created_at' => '2014-07-09 09:45:50',
                        'updated_at' => 'NULL',
                        'deleted_at' => NULL,
                        'children' => Array
                            (
                            )
                    )
            )
    )
)

我正在使用的recursive function将记录存储到数据库中,如下所示:

The recursive function I am using store the records into the database is as below :

public function addRecursiveChildren(array $surveychildren, $parentid, $userid){

    foreach ($surveychildren as $item) 
    {
        /* Error : HTTPRequest Error :: 500: {"error":{"type":"ErrorException","message":"Cannot use a scalar value as an array
           Error is in the statement below in the second recursion when child is passes as an input.
        */
        $item['survey_id'] = $item['id']; 
        $item['user_id'] = $userid;
        $item['id'] = null; 
        $item['parent'] = $parentid; 

        $routine = routine::create($item);

        if(count($item["children"]) > 0)
        {
            foreach ($item["children"] as $child) 
            {
                /* The $child I found is as below : 
                $child = array(
                    'id' => 562,
                    'type' => 'o',
                    'subtype' => NULL ,
                    'title' => 'Yes',
                    'parent' => 561,
                    'created_at' => '2014-07-09 09:45:50',
                    'updated_at' => NULL,
                    'deleted_at' => NULL,
                    'children' => Array
                        (
                        )
                );
                */

                RoutineRepository::addRecursiveChildren($child, $routine->id, $userid);
            }
        }
    }
}

我知道错误原因是$child我正在作为input array传递给上面的recursive function:

I know that cause of error is the $child I am passing as an input array to the recursive function above :

$child是这样的:

array(
    'id' => 562,
    'type' => 'o',
    'subtype' => NULL ,
    'title' => 'Yes',
    'parent' => 561,
    'created_at' => '2014-07-09 09:45:50',
    'updated_at' => NULL,
    'deleted_at' => NULL,
    'children' => Array
    (
    )
)

如果$child将是这样,则代替此:

Instead of this if $child will be something like this :

Array
(
    [0] =>
    array(
        'id' => 562,
        'type' => 'o',
        'subtype' => NULL ,
        'title' => 'Yes',
        'parent' => 561,
        'created_at' => '2014-07-09 09:45:50',
        'updated_at' => NULL,
        'deleted_at' => NULL,
        'children' => Array
        (
        )
    )
)

那么就不会有错误.

有人可以帮助我克服它吗?

Can anybody help me to overcome it?

谢谢.

推荐答案

这应该有效

class Routine extends \Eloquent
{
    // The relation
    public function subRoutine()
    {
        return $this->hasMany('Routine', 'parent');
    }

    public function saveSubroutine(array $children)
    {
        foreach($children as $childData)
        {
            $child = new self($childData);
            $this->subRoutine()->save($child);

            $child->saveSubroutine($childData['children']);
        }
    }
}

$routine = Routine::create($data);
$routine->saveSubroutine($data['children']);

这篇关于带有嵌套数组的PHP Laravel递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆