格式化多维数组

时间:2016-08-18 14:08:43

标签: php arrays laravel multidimensional-array

我通过这样做得到了一个Project Objects列表

grep -A1 bbbbbbb | grep -v bbbbbbb

上面的输出是这样的

$projects = Project::orderBy('deploymentDate', 'ASC')->get();

我删除了很多不必要的数据。所以你可以看到我有两个项目。问题是,这些项目与其他模型有关系,所以我需要创建一个包含所有这些数据的数组。所以如果我这样做

Collection {#292
  #items: array:2 [
    0 => Project {#293
      #attributes: array:15 [
        "id" => "4"
        "name" => "Something"
        "value" => "234"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "sdfsd"
      ]
    }
    1 => Project {#294
      #attributes: array:15 [
        "id" => "3"
        "name" => "Something"
        "value" => "6"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "John Doe"
      ]
    }
  ]
}

我可以看到这个

foreach($projects as $project) {
    dd($project->projectType);
}

所以我创建了一个数组,并计划将项目及其关系添加到此数组中。目前我正在尝试这个

#attributes: array:6 [
    "id" => "2"
    "additionalInformation" => null
    "project_id" => "4"
]

这个问题是它输出以下

$projectsArray = array();
foreach($projects as $project) {
    $projectsArray[] = array(
        "Project" => json_decode($project, true)
    );

    if($project->projectType) {
        $projectsArray[] = array(
            "Types" => json_decode($project->projectType, true)
        );
    }
}

所以它将数据添加到数组中,但不将其嵌套为关系。理想情况下,我会为每个项目追求这样的事情

array:4 [
  0 => array:1 [
    "Project" => array:15 [
       "id" => "4"
        "name" => "Something"
        "value" => "234"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "sdfsd"
    ]
  ]
  1 => array:1 [
    "Types" => array:6 [
       "id" => 2
      "additionalInformation" => null
      "project_id" => "4"
    ]
  ]
  2 => array:1 [
    "Project" => array:15 [
     "id" => "3"
        "name" => "Something"
        "value" => "6"
        "user_id" => "1"
        "client_id" => "97"
        "contact" => "John Doe"
    ]
  ]
  3 => array:1 [
    "Types" => array:6 [
      "id" => 1
      "additionalInformation" => null
      "project_id" => "3"
    ]
  ]
]

如何实现这一目标?

由于

3 个答案:

答案 0 :(得分:1)

完成后,只需在数组中创建$ project var和array_push(主数据,然后添加相关数据)。

$newProject = array();
$newProject["Project"] = json_decode($project, true);
if($project->projectType) {
    $newProject["Types"] = json_decode($project->projectType, true);
}

array_push($projectsArray, $newProject);
// of $projectArray[] = $newProject;

答案 1 :(得分:1)

您的问题在于如何构建阵列。你这样做:

$projectsArray = array();
foreach($projects as $project) {
// Add [Project => []] to $projectsArray
    $projectsArray[] = array(
        "Project" => json_decode($project, true)
    );
// $projectsArray now has one element, which is an array

    if($project->projectType) {
// Add [Types => []] to $projectsArray
        $projectsArray[] = array(
            "Types" => json_decode($project->projectType, true)
        );
// $projectsArray now has two array elements, each with a single key
    }
}

听起来你想要的是将Types键/值对添加到json_decode生成的数组中,在这种情况下,您需要将其标识为可添加的独特内容,如下所示:

$projectsArray = array();
foreach($projects as $project) {
    $projectarr = array(
        "Project" => json_decode($project, true)
    );

    if($project->projectType) {
        $projectarr["Types"] = json_decode($project->projectType, true);
    }
    $projectsArray[] = $projectarr;
}

答案 2 :(得分:-1)

你为什么不尝试这样的事情:

$projectsArray = array();
foreach($projects as $project) {
    $projectTmp = json_decode($project, true);
    if($project->projectType) {
        $projectTmp["Types"] = json_decode($project->projectType, true);
    }
    $projectsArray[] = array("Project" => $projectTmp);
}