Kerim Kerim - 3 months ago 28
JSON Question

Json common key make group in symfony

I have a json. My json have common keys. I want to show common keys in one json. Now my json like this :

{
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"
}
]
}


I want to collect below in academicYear key, if academicKeys are equal.
I hope so I could explain my problem.

My code here :

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'));
}


When I tried 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

Answer

What you have is a simple group by functionality, this could be abstracted to be reusable, though if needed only in this specific instance, here's an example of how you could do it:

$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);

Demo: https://3v4l.org/kqsQl