在PHP中创建对象和数组的复杂JSON嵌套

时间:2015-05-19 10:53:13

标签: php mysql json

我的表格如下:

enter image description here

我需要使用以下结构创建JSON对象:

    array=[
  {
    id: 2,
    items: [
      {
        id: 12
      },
      {
        id: 13
      },
      {
        id: 14
      }
    ]
  },
  {
    id: 3,
    items: [
      {
        id: 15
      },
      {
        id: 16
      },
      {
        id: 17
      },
      {
        id: 18
      },
      {
        id: 19
      }
    ]
  },
  {
    id: 4,
    items: [
      {
        id: 20
      },
      {
        id: 21
      }
    ]
  },
  {
    id: 5,
    items: [
      {
        id: 22
      }
    ]
  }
];

因此,对象必须是父ID,并且其项目必须是子ID。我必须使用哪个查询,如何创建类似的结构?它也取消了这些对象的顺序。必须订购父母和孩子。我的意思是,在我的查询中必须像这样:

ORDER BY `order` ASC

提前致谢。

1 个答案:

答案 0 :(得分:0)

自我加入可能是这项工作的解决方案。尝试像

这样的东西
| Downloading: antlr-3.2.pom.sha1
sha1 OK for http://repo1.maven.org/maven2/org/antlr/antlr/3.2/antlr-3.2.pom
    [SUCCESSFUL ] org.antlr#antlr;3.2!antlr.pom(pom.original) (361ms)
cache: Checking cache for: dependency: org.antlr#antlr-master;3.2 {}
        tried /home/user/Projects/ellucian_code/banner_student_faculty_grade_entry_app/lib/antlr-master-3.2.jar
    grailsPlugins: no ivy file nor artifact found for org.antlr#antlr-master;3.2
        tried /home/user/.gvm/grails/2.2.1/lib/org.antlr/antlr-master/ivy-3.2.xml
        tried /home/user/.gvm/grails/2.2.1/lib/org.antlr/antlr-master/jars/antlr-master-3.2.jar
        tried /home/user/.gvm/grails/2.2.1/lib/org.antlr/antlr-master/bundles/antlr-master-3.2.jar
    grailsHome: no ivy file nor artifact found for org.antlr#antlr-master;3.2
        tried /home/user/.gvm/grails/2.2.1/src/libs/antlr-master-3.2.xml
        tried /home/user/.gvm/grails/2.2.1/src/libs/antlr-master-3.2.jar
    grailsHome: no ivy file nor artifact found for org.antlr#antlr-master;3.2
        tried /home/user/.gvm/grails/2.2.1/dist/antlr-master-3.2.xml
        tried /home/user/.gvm/grails/2.2.1/dist/antlr-master-3.2.jar
    grailsHome: no ivy file nor artifact found for org.antlr#antlr-master;3.2
        tried /home/user/.grails/2.2.1/cached-installed-plugins/antlr-master-3.2.xml
        tried /home/user/.grails/2.2.1/cached-installed-plugins/antlr-master-3.2.jar
    grailsHome: no ivy file nor artifact found for org.antlr#antlr-master;3.2
don't use cache for org.antlr#antlr-master;3.2: checkModified=true
        tried /home/user/.gvm/grails/2.2.1/plugins/antlr-master-3.2.pom
        tried /home/user/.gvm/grails/2.2.1/plugins/antlr-master-3.2.jar
    grailsHome: no ivy file nor artifact found for org.antlr#antlr-master;3.2
don't use cache for org.antlr#antlr-master;3.2: checkModified=true
        tried http://grails.org/plugins/grails-antlr-master/tags/RELEASE_3.2/antlr-master-3.2.pom
        tried http://grails.org/plugins/grails-antlr-master/tags/RELEASE_3.2/grails-antlr-master-3.2.jar
    grailsCentral: no ivy file nor artifact found for org.antlr#antlr-master;3.2
        tried http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.pom
    mavenCentral: found md file for org.antlr#antlr-master;3.2
        => http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.pom (3.2)
downloading http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.pom ...
    mavenCentral: downloading http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.pom
| Downloading: antlr-master-3.2.pom
    mavenCentral: downloading http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.pom.sha1
| Downloading: antlr-master-3.2.pom.sha1
sha1 OK for http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.pom
    [SUCCESSFUL ] org.antlr#antlr-master;3.2!antlr-master.pom(pom.original) (327ms)
        tried /home/user/Projects/ellucian_code/banner_student_faculty_grade_entry_app/lib/antlr-master-3.2.jar
        tried /home/user/.gvm/grails/2.2.1/lib/org.antlr/antlr-master/jars/antlr-master-3.2.jar
        tried /home/user/.gvm/grails/2.2.1/lib/org.antlr/antlr-master/bundles/antlr-master-3.2.jar
        tried /home/user/.gvm/grails/2.2.1/src/libs/antlr-master-3.2.jar
        tried /home/user/.gvm/grails/2.2.1/dist/antlr-master-3.2.jar
        tried /home/user/.grails/2.2.1/cached-installed-plugins/antlr-master-3.2.jar
        tried /home/user/.gvm/grails/2.2.1/plugins/antlr-master-3.2.jar
        tried http://grails.org/plugins/grails-antlr-master/tags/RELEASE_3.2/grails-antlr-master-3.2.jar
        tried http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repo1.maven.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.jboss.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.jboss.org/maven2/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.codehaus.org/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repo.grails.org/grails/plugins/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repo.grails.org/grails/plugins/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
CLIENT ERROR: Not Found url=http://repository.springsource.com/maven/bundles/external/org/antlr/antlr-master/3.2/antlr-master-3.2.jar
        tried http://repository.springsource.com/maven/bundles/release/org/antlr/antlr-master/3.2/antlr-master-3.2.jar