symfony

时间:2016-08-25 07:26:36

标签: php json

我有一个json。我的json有共同的钥匙。我想在一个json中显示常用键。现在我的json喜欢这个:

 {
 data: [
    {
     updatedDate: null,
     active: true,
     id: 80,
     lessonName: "HZİ 413 Language in Use",
     educationDegree: "Lisans",
     academicYear: "2013-2014",
     language: "İngilizce",
     lessonHour: 4,
     addedDate: "2014-02-21T19:54:23+0000"
    },
    {
    updatedDate: null,
    active: true,
    id: 81,
    lessonName: "HZİ 411 English in Context I",
    educationDegree: "Lisans",
    academicYear: "2013-2014",
    language: "İngilizce",
    lessonHour: 8,
    addedDate: "2014-02-21T19:53:47+0000"
    },
    {
    updatedDate: null,
    active: true,
    id: 82,
    lessonName: "HZİ 512 English in Context II ",
    educationDegree: "Lisans",
    academicYear: "2014-2015",
    language: "İngilizce",
    lessonHour: 12,
    addedDate: "2014-02-21T19:52:44+0000"
    }
 ]
}

如果学术成绩相同,我想在学术年鉴中收集。 我希望我可以解释我的问题。

我的代码在这里:

    public function indexAction()
{
    $data = $this->get('service.lesson')->get();

    $data=json_decode($data,true);

    $out = [];

    foreach($data as $element) {
        $out[$element['academicYear']][] =
            [
            'lessonName'      => $element['lessonName'],
            'lessonHour'      => $element['lessonHour'],
            'educationDegree' => $element['educationDegree'],
        ];
    }

    var_dump(json_encode($out, JSON_PRETTY_PRINT));

    return $this->createApiResponse(compact('data'));
}

当我尝试var_dump($ data);

array (size=3)
0 => 
object(AppBundle\Entity\Yoksis\Lesson)[1637]
  private 'id' => int 80
  private 'lessonName' => string 'HZİ 413 Language in Use' (length=24)
  private 'educationDegree' => 
    object(Proxies\__CG__\AppBundle\Entity\Yoksis\YokParameter)[1654]
      public '__initializer__' => 
        object(Closure)[1646]
          ...
      public '__cloner__' => 
        object(Closure)[1647]
          ...
      public '__isInitialized__' => boolean false
      private 'id' (AppBundle\Entity\Yoksis\YokParameter) => int 127
      private 'yoksisId' (AppBundle\Entity\Yoksis\YokParameter) => null
      private 'name' (AppBundle\Entity\Yoksis\YokParameter) => null
      private 'parameterType' (AppBundle\Entity\Yoksis\YokParameter) => null
  private 'academicYear' => 
    object(Proxies\__CG__\AppBundle\Entity\Yoksis\YokParameter)[1653]
      public '__initializer__' => 
        object(Closure)[1646]
          ...
      public '__cloner__' => 
        object(Closure)[1647]
          ...
      public '__isInitialized__' => boolean false
      private 'id' (AppBundle\Entity\Yoksis\YokParameter) => int 509
      private 'yoksisId' (AppBundle\Entity\Yoksis\YokParameter) => null
      private 'name' (AppBundle\Entity\Yoksis\YokParameter) => null
      private 'parameterType' (AppBundle\Entity\Yoksis\YokParameter) => null
  private 'language' => 
    object(Proxies\__CG__\AppBundle\Entity\Yoksis\Language)[1665]
      public '__initializer__' => 
        object(Closure)[1657]
          ...
      public '__cloner__' => 
        object(Closure)[1658]
          ...
      public '__isInitialized__' => boolean false
      private 'id' (AppBundle\Entity\Yoksis\Language) => int 83
      private 'name' (AppBundle\Entity\Yoksis\Language) => null
      private 'yoksisId' (AppBundle\Entity\Yoksis\Language) => null
      private 'code' (AppBundle\Entity\Yoksis\Language) => null
  private 'lessonHour' => int 4
  private 'addedDate' => 
    object(DateTime)[1632]
      public 'date' => string '2014-02-21 19:54:23.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)
  private 'user' (AppBundle\Entity\Yoksis\YoksisEntity) => 
    object(AppBundle\Entity\User)[1286]
      private 'id' => int 30
      private 'tcId' => string '********' (length=11)
      private 'email' => string '******' (length=7)
      private 'firstName' => string '*****' (length=5)
      private 'lastName' => string '*****' (length=7)
      private 'roles' => 
        object(Doctrine\ORM\PersistentCollection)[1309]
          ...
      private 'phone' => null
      private 'mobile' => null
      private 'userType' => 
        object(Proxies\__CG__\AppBundle\Entity\UserType)[1319]
          ...
      private 'personnelTitle' => 
        object(Proxies\__CG__\AppBundle\Entity\PersonnelTitle)[1329]
          ...
      private 'assignmentTitle' => 
        object(Proxies\__CG__\AppBundle\Entity\AssignmentTitle)[1339]
          ...
      private 'administrativeTitles' => 
        object(Doctrine\ORM\PersistentCollection)[1358]
          ...
      private 'personnelDepartment' => 
        object(Proxies\__CG__\AppBundle\Entity\Department)[1368]
          ...
      private 'assignmentDepartmentGroups' => 
        object(Doctrine\ORM\PersistentCollection)[1384]
          ...
      private 'phoneBookDepartments' => 
        object(Doctrine\ORM\PersistentCollection)[1385]
          ...
      private 'menus' => 
        object(Doctrine\ORM\PersistentCollection)[1414]
          ...
      private 'notifications' => 
        object(Doctrine\ORM\PersistentCollection)[1439]
          ...
      protected 'createdAt' => 
        object(DateTime)[1282]
          ...
      protected 'updatedAt' => 
        object(DateTime)[1283]
          ...
      protected 'deletedAt' => null
  private 'yoksisId' (AppBundle\Entity\Yoksis\YoksisEntity) => int 423309
  private 'updatedDate' (AppBundle\Entity\Yoksis\YoksisEntity) => null
  private 'active' (AppBundle\Entity\Yoksis\YoksisEntity) => boolean true
  protected 'createdAt' => 
    object(DateTime)[1633]
      public 'date' => string '2016-08-25 06:27:46.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)
  protected 'updatedAt' => 
    object(DateTime)[1634]
      public 'date' => string '2016-08-25 06:27:46.000000' (length=26)
      public 'timezone_type' => int 3
      public 'timezone' => string 'UTC' (length=3)
  protected 'deletedAt' => null

1 个答案:

答案 0 :(得分:0)

你所拥有的是一个简单的 group by 功能,这可以被抽象为可重用的,尽管只在这个特定的例子中需要,这里有一个如何做到这一点的例子:

$groups = [];

foreach ($input as $element) {
    $groupValue = $element['academicYear'];

    # or in your case, something like:
    # $groupValue = $element->getAcademicYear()->getName();

    if (!array_key_exists($groupValue, $groups)) {
        $groups[$groupValue] = [];
    }

    $groups[$groupValue][] = $element;
}

print_r($groups);

演示:https://3v4l.org/kqsQl

相关问题