samedi 22 juin 2019

Repeating objects within array php

I am trying to get a specific output for a API I am creating however one of the joins I am extracting needs a new key value assigned to it so I am putting it into a for loop. However due to the output being assigned to a new array the values repeat, I have tried stating that only values not in the array can be assigned but it still does not solve my problem. If anyone could be of assistance it would be great.

 public function showAllTeamInformation()
{
    // get the team id 
    $teamid = Teams::all()->pluck('team_id')->toArray();
    // get all the team info 
    $teaminfomratin = Teams::all()->toArray();
    // the complete loop by team id nu,ber
    foreach ($teamid as $number) {
     // get the skill name and scor  
    $skills[] = Teams::find($number)->skills->pluck('skill_name', 
     'score')->toArray();
    // get the hobbies 
    $hobbies[] = Teams::find($number)->hobbies->pluck('hobby');
    $final =array();
    // for loop to assignemtn new key value 
    foreach($skills as $row => $value){
        foreach($value as $rows => $values){
            if (!in_array($values, $final))
             {
              $final['skills'][] =  array('name'=>$values , 'skill' => $rows);
             }
         }
     }  

    foreach($hobbies as $row => $value){
        $newArrays['hobbies'] =  $value;
    }
    // merging them all together
   $output_array[$number-1] = array_merge($teaminfomratin[$number- 
    1], $final, $newArrays);

}


    return response()->json($output_array);

}

The current output is

            [
            {
            "team_id": 1,
            "first_name": "jack",
            "surname": "churchill",
            "nickname": "jacko",
            "role": "developer",
            "image_url": "jack.jpg",
            "team": "production",
            "company": "chelsea apps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "mysql",
            "skill": 4
            },
            {
            "name": "php",
            "skill": 5
            }
            ],
            "hobbies": [
            "running",
            "more"
            ]
            },
            [
            {
            "team_id": 1,
            "first_name": "jack",
            "surname": "churchill",
            "nickname": "jacko",
            "role": "developer",
            "image_url": "jack.jpg",
            "team": "production",
            "company": "chelsea apps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "mysql",
            "skill": 4
            },
            {
            "name": "php",
            "skill": 5
            }
            ],
            "hobbies": [
            "running",
            "more"
            ]
            },
            {
            "team_id": 2,
            "first_name": "marco",
            "surname": "",
            "nickname": "marco",
            "role": "frontend",
            "image_url": "marco.jpg",
            "team": "dev",
            "company": "chelseaapps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "mysql",
            "skill": 4
            },
            {
            "name": "php",
            "skill": 5
            },
            {
            "name": "sass",
            "skill": 4
            }
            ],
            "hobbies": [
            "running",
            "netflix"
            ]
            },
            {
            "team_id": 3,
            "first_name": "harry",
            "surname": "smith",
            "nickname": "harry ",
            "role": "marketing",
            "image_url": "harry.jpg",
            "team": "marketing",
            "company": "chelsea apps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "mysql",
            "skill": 4
            },
            {
            "name": "php",
            "skill": 5
            },
            {
            "name": "sass",
            "skill": 4
            },
            {
            "name": "being cool",
            "skill": 5
            }
            ],
            "hobbies": [
            "going out"
            ]
            }
            ]

As you can see the skills are copied to the next object which is not right.

The correct output would be for team 2 and 3 for example would be

            [
            {
            "team_id": 1,
            "first_name": "jack",
            "surname": "churchill",
            "nickname": "jacko",
            "role": "developer",
            "image_url": "jack.jpg",
            "team": "production",
            "company": "chelsea apps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "mysql",
            "skill": 4
            },
            {
            "name": "php",
            "skill": 5
            }
            ],
            "hobbies": [
            "running",
            "more"
            ]
            },
            {
            "team_id": 2,
            "first_name": "marco",
            "surname": "",
            "nickname": "marco",
            "role": "frontend",
            "image_url": "marco.jpg",
            "team": "dev",
            "company": "chelseaapps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "sass",
            "skill": 4
            }
            ],
            "hobbies": [
            "running",
            "netflix"
            ]
            },
            {
            "team_id": 3,
            "first_name": "harry",
            "surname": "smith",
            "nickname": "harry ",
            "role": "marketing",
            "image_url": "harry.jpg",
            "team": "marketing",
            "company": "chelsea apps",
            "created_at": null,
            "updated_at": null,
            "skills": [
            {
            "name": "being cool",
            "skill": 5
            }
            ],
            "hobbies": [
            "going out"
            ]
            }
            ]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire