为什么对象值在其他对象中重复,我的json响应是这个

时间:2018-02-07 07:59:11

标签: php json

foreach($ user_joined_players为$ user_joined_player){

            foreach ($batting_live_scores as $batting_live_score) {
                if (in_array($batting_live_score['player_id'], explode(',', $user_joined_player['players_id']))) {
                    $player_data = array();
                    $player_data['player_id'] = $batting_live_score['player_id'];
                    $player_data['one'] = $batting_live_score['one'];
                    $p_data[] = $player_data;
                }
            }
            $user_data['user_name'] = $user_joined_player['fname'];
            $user_data['user_players'] = $p_data;

            $data[] = $user_data;
        }
        $json = array("status" => "success", "data" => $data);

{     “地位”:“成功”,     “数据”:[         {             “user_name”:“Harshn”,             “user_players”:[                 {                     “player_id”:“2”,                     “一”:“2​​”                 }             ]         },         {             “user_name”:“测试”,             “user_players”:[                 {                     “player_id”:“2”,                     “一”:“2​​”                 },                 {                     “player_id”:“6”,                     “一”:“0”                 }             ]         },         {             “user_name”:“NilSan”,             “user_players”:[                 {                     “player_id”:“2”,                     “一”:“2​​”                 },                 {                     “player_id”:“6”,                     “一”:“0”                 },                 {                     “player_id”:“1”,                     “一”:“1”                 }             ]         },         {             “user_name”:“Raaz”,             “user_players”:[                 {                     “player_id”:“2”,                     “一”:“2​​”                 },                 {                     “player_id”:“6”,                     “一”:“0”                 },                 {                     “player_id”:“1”,                     “一”:“1”                 },                 {                     “player_id”:“1”,                     “一”:“1”                 }             ]         }     ] }

1 个答案:

答案 0 :(得分:-1)

foreach ($batting_live_scores as $batting_live_score) {
    if (in_array($batting_live_score['player_id'], explode(',', $user_joined_player['players_id']))) {
        $player_data = array();
        $player_data['player_id'] = $batting_live_score['player_id'];
        $player_data['one'] = $batting_live_score['one'];
        $p_data[] = $player_data;
    }
}