2017-04-23 2 views
0

ムービースケジュールに関するAPIを取得しようとしています。しかし、json出力は私が望むものではありませんでした。映画でソートされていない劇場でソートしたいのですが。だから、私はjsonの価値の位置を変更する必要がありますか?この私がAPI私はAPIからのPHP JSON位置の変更

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

それとも劇場でソートされた第一の出力JSONを作るための可能な方法があるにしたいものをJSON

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

そしてここから得た出力JSON。何か案が?

答えて

3

これを試してみてください:

// 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; 
} 
関連する問題