dimanche 7 avril 2019

Laravel JSON response changes value of associative array key

I've defined a response format for data in that Laravel should return in the front-end with the following code:

$result1=$spaceRole->pluck('role','space.id')->all();
$resultToReturn=[];
foreach($result1 as $key=>$value){
    $resultToReturn[$key]=array($value);
}
 return $resultToReturn;

And the code gives me the following result as expected:

 [                       
   850 => [              
     "AUTHORS",          
   ],                    
   766 => [              
     "ADMINISTRATORS",   
   ],                    
   844 => [              
     "ADMINISTRATORS",   
   ],                    
   0 => [                
     "ADMINISTRATORS",   
   ],                    
 ] 

And i would like to keep the same format in my json response.But i the data returned in JSON,the format changes like this :

0: ["AUTHORS"]
1: ["ADMINISTRATORS"]
2: ["ADMINISTRATORS"]
3: ["ADMINISTRATORS"]

Now the indexes are 0,1,2,3.Where has 850,844,0 and 766 gone?I would like to use them and not those indexes? Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire