2017-04-23 1 views
0

J'essaie d'obtenir une API sur l'horaire des films. Mais la sortie de JSON n'était pas comme je le veux. Je veux faire trier par Theatre, pas trié par film. Donc, je devrais changer la position de la valeur json, est-ce possible? Ce JSON de sortie je suis arrivé de l'APIPHP JSON de l'API Changement de position

{ 
    "data": [ 
     { 
      "movie": "movie A", 
      "schedule": [ 
       { 
        "theater": "theater A", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.60,000" 
       }, 
       { 
        "theater": "theater B", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.50,000" 
       } 
      ] 
     } 
     { 
      "movie": "movie B", 
      "schedule": [ 
       { 
        "theater": "theater A", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.60,000" 
       }, 
       { 
        "theater": "theater B", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.50,000" 
       } 
      ] 
     } 
    ] 
} 

Et voici ce que JSON que je veux avoir

{ 
    "data": [ 
     { 
      "theater": "theater A", 
      "schedule": [ 
       { 
        "movie": "Movie A", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.60,000" 
       }, 
       { 
        "movie": "Movie B", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.50,000" 
       } 
      ] 
     } 
     { 
      "theater": "theater B", 
      "schedule": [ 
       { 
        "movie": "Movie A", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.60,000" 
       }, 
       { 
        "movie": "Movie B", 
        "time": [ 
         "13:00", 
         "15:00", 
         "17:00", 
         "19:00", 
         "21:00" 
        ], 
        "price": "Rp.50,000" 
       } 
      ] 
     } 
    ] 
} 

Ou peut-être il y a moyen possible de faire la première JSON de sortie triés par le théâtre. Une idée?

Répondre

3

Essayez ceci:

// JSONfrom api is stored in $apiJSON 
$apiArr = json_decode($apiJSON, true); 
$tmpArr = array(); 
foreach ($apiArr['data'] as $data) { 
    $movie = $data['movie']; 
    foreach ($data['schedule'] as $schedule) { 
     $tmpArr[$schedule['theater']][] = array('movie'=>$movie,'time'=>$schedule['time'],'price'=>$schedule['price']); 
    } 
} 

// Create new array structure 
$newArr = array(); 
foreach ($tmpArr as $theater=>$movies) { 
    $tmp = array(); 
    $tmp['theater'] = $theater; 
    $tmp['schedule'] = $movies; 
    $newArr['data'][] = $tmp; 
}