重构嵌套的JSON数据

时间:2016-12-12 10:27:16

标签: php json

我有以下JSON:

{
    "filaria": [{
        "user_input_test_result": "",
        "test_id": "3519710705971061481279327210",
        "gid": "1481279304461"
    }, {
        "user_input_test_result": "",
        "test_id": "3519710705971061481279364547",
        "gid": "1481279342277"
    }],
    "patient_test_analysis": [{

        "test_id": "3519710705971061481279327210",
        "gid": "1481279304461"

    }, {
        "test_id": "3519710705971061481279364547",
        "gid": "1481279342277"

    }]
}

我必须根据密钥名gid重新构建此JSON,以便filariapatient_test_analysis数据成为相应gid值的子数组,如这样:

{
    "upload_status": {
        "1481279304461": {
            "filaria": [{
                "status": "Success",
                "test_id": "3519710705971061481279327210"
            }],
            "patient_test_analysis": [{
                "status": "Success",
                "test_id": "3519710705971061481279327210"
            }]
        },
        "1481279342277": {
            "filaria": [{
                "status": "Success",
                "test_id": "3519710705971061481279364547"
            }],
            "patient_test_analysis": [{
                "status": "Success",
                "test_id": "3519710705971061481279364547"
            }]
        }
    }
}

每个子数组还必须添加"status": "Success"元素。

我编写了以下代码来获取输出:

$jsonArray = json_decode($json, true);
$arrayStatus = array();

foreach ($jsonArray as $key => $value) {
    $key; $inc = 0 ;
    for($i=0;$i<count($value);$i++){ 
        $patient_id = $value[$i]['gid'];
        $s = $inc++;
        $arrayStatus[$patient_id][$key][$s]['status'] = "Success";
        $arrayStatus[$patient_id][$key][$s]['test_id'] = 'xxx';     
    }     
}         

$results = array();    
$results['upload_status'] = $arrayStatus; 
echo $resArr = json_encode($results);

但我得到以下输出:

{
    "upload_status": {
        "1481279304461": {
            "filaria": [{
                "status": "Success",
                "test_id": "3519710705971061481279327210"
            }],
            "patient_test_analysis": [{
                "status": "Success",
                "test_id": "3519710705971061481279327210"
            }]
        },
        "1481279342277": {
            "filaria": {
                "1": {
                    "status": "Success",
                    "test_id": "3519710705971061481279364547"
                }
            },
            "patient_test_analysis": {
                "1": {
                    "status": "Success",
                    "test_id": "3519710705971061481279364547"
                }
            }
        }
    }
}

在第二个gid中具有额外的间接级别。

我不知道我哪里出错了。

1 个答案:

答案 0 :(得分:1)

试试这个:

// vendor\pugx\multi-user-bundle\Doctrine\UserManager.php (function __construct)

 public function __construct(PasswordUpdaterInterface $passwordUpdater, CanonicalFieldsUpdater $canonicalFieldsUpdater, ObjectManager $om, $class, UserDiscriminator $userDiscriminator)
    {
        $this->om = $om;
        $this->userDiscriminator = $userDiscriminator;

        parent::__construct($passwordUpdater, $canonicalFieldsUpdater, $om, $class);
    }