Как получить доступ к каждому значению из ассоциативного массива с пробелом в ключе - PullRequest
0 голосов
/ 11 ноября 2019

Я хочу получить доступ к каждому значению в моем массиве json и сохранить эти данные в базе данных MySQL, используя php:

{
    "api": {
        "results": 16,
        "statistics": {
            "Shots on Goal": {
                "home": "5",
                "away": "2"
            },
            "Shots off Goal": {
                "home": "7",
                "away": "4"
            },
            .....
            "Passes %": {
                "home": "91%",
                "away": "88%"
            }
        }
    }
}

Я использую php и пробовал разные решения json_decode ($ result, true)и {'Shots on Goal'}, но это показывает ошибки и иногда дает все значения, которые я хочу, чтобы индивидуально получить доступ ко всем значениям

брат, прежде всего большое спасибо, и это мой полный массив, пожалуйста, предложите мне, что я делаю. {"api": {"results": 16, "statistics": {"Удары по воротам": {"home": "5", "away": "2"}, "Удары по воротам": {"home ":" 7 "," away ":" 4 "}," Total Shots ": {" home ":" 17 "," away ":" 8 "}," Blocked Shots ": {" home ":"5 "," away ":" 2 "}," Shots insidebox ": {" home ":" 12 "," away ":" 5 "}," Shots outsidebox ": {" home ":" 5 ","away ":" 3 "}," Fouls ": {" home ":" 15 "," away ":" 14 "}," Угловые удары ": {" home ":" 9 "," away ":" 1"}," Offsides ": {" home ":" 2 "," away ":" 2 "}," Владение мячом ": {" home ":" 61% "," away ":" 39% "},"Желтые карточки": {"home": "2", "away": "3"}, "Red Cards": {"home ":" "," away ":" "}," Вратарь спасает ": {" home ":" 1 "," away ":" 4 "}," Всего пропущено ": {" home ":" 633 ", "away": "414"}, "Passes точно": {"home": "575", "away": "365"}, "Passes%": {"home": "91%", "away":" 88% "}}}} это мой полный массив

Ответы [ 2 ]

0 голосов
/ 11 ноября 2019
$va = '{ "api": { "results": 16, "statistics": { 
"Shots on Goal": 
{ "home": "5", "away": "2" },

 "Shots off Goal":
 { "home": "7", "away": "4" }, 
"Total Shots": { "home": "17", "away": "8" },
 "Blocked Shots": { "home": "5", "away": "2" },
 "Shots insidebox": { "home": "12", "away": "5" }, 
"Shots outsidebox": { "home": "5", "away": "3" }, "Fouls": { "home": "15", "away": "14" }, "Corner Kicks": { "home": "9", "away": "1" },
 "Offsides": { "home": "2", "away": "2" }, "Ball Possession": { "home": "61%", "away": "39%" }, 
"Yellow Cards": { "home": "2", "away": "3" }, "Red Cards": { "home": "", "away": "" }, 
"Goalkeeper Saves": { "home": "1", "away": "4" }, "Total passes": { "home": "633", "away": "414" }, 
"Passes accurate": { "home": "575", "away": "365" }, "Passes %": { "home": "91%", "away": "88%" } } } } ';
$data = json_decode($va);
$sqlinsert = "INSERT INTO fixture_statistics (Shots_on_Goal_home, 
Shots_on_Goal_away, Shots_of_Goal_home, Shots_of_Goal_away, Total_Shots_home, Total_Shots_away, Blocked_Shots_home, Blocked_Shots_away, Shots_insidebox_home, Shots_insidebox_away, Shots_outsidebox_home, Shots_outsidebox_away, Fouls_home, Fouls_away, Corner_Kicks_home, Corner_Kicks_away, Offsides_home, Offsides_away, Ball_Possession_home, Ball_Possession_away, Yellow_Cards_home, Yellow_Cards_away, Red_Cards_home, Red_Cards_away, Goalkeeper_Saves_home, Goalkeeper_Saves_away, Total_passes_away, Passes_percentage_away)";
$insert_value='';
foreach($data->api->statistics as $key=> $value){
    if($key == 'Shots on Goal'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
    elseif($key == 'Total Shots'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
    elseif($key == 'Blocked Shots'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
    elseif($key == 'Shots insidebox'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
     elseif($key == 'Shots outsidebox'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
     elseif($key == 'Fouls'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
     elseif($key == 'Corner Kicks'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
       elseif($key == 'Offsides'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
       elseif($key == 'Ball Possession'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
       elseif($key == 'Yellow Cards'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
       elseif($key == 'Red Cards'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }

       elseif($key == 'Goalkeeper Saves'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }
       elseif($key == 'Total passes'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }


       elseif($key == 'Passes accurate'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }

       elseif($key == 'Passes %'){
        $insert_value.="'".$value->home."', '".$value->away."',";
    }

}

$final_insert_query =  rtrim($insert_value, ',');
$sqlinsert.='VALUES('.$final_insert_query.")";
echo $sqlinsert;

Он сгенерирует этот запрос для вашей таблицы.

INSERT INTO fixture_statistics (Shots_on_Goal_home, 
Shots_on_Goal_away, Shots_of_Goal_home, Shots_of_Goal_away, Total_Shots_home, Total_Shots_away, Blocked_Shots_home, Blocked_Shots_away, Shots_insidebox_home, Shots_insidebox_away, Shots_outsidebox_home, Shots_outsidebox_away, Fouls_home, Fouls_away, Corner_Kicks_home, Corner_Kicks_away, Offsides_home, Offsides_away, Ball_Possession_home, Ball_Possession_away, Yellow_Cards_home, Yellow_Cards_away, Red_Cards_home, Red_Cards_away, Goalkeeper_Saves_home, Goalkeeper_Saves_away, Total_passes_away, Passes_percentage_away)VALUES('5', '2','17', '8','5', '2','12', '5','5', '3','15', '14','9', '1','2', '2','61%', '39%','2', '3','', '','1', '4','633', '414','575', '365','91%', '88%')
0 голосов
/ 11 ноября 2019

Это сделает запрос вставки, Это будет работать, я проверял.

$va = '{
    "api": {
        "results": 16,
        "statistics": {
            "Shots on Goal": {
                "home": "5",
                "away": "2"
            },
            "Shots off Goal": {
                "home": "7",
                "away": "4"
            },
            "Passes %": {
                "home": "91%",
                "away": "88%"
            }
        }
    }
}';
$data = json_decode($va);
$sqlinsert = "INSERT INTO TABLE_NAME (column1, column2, column3)";
$insert_value='';
foreach($data->api->statistics as $value){
    $insert_value.="('".$value->home."', '".$value->away."', '".$data->api->results."'),";
}

$final_insert_query =  rtrim($insert_value, ',');
$sqlinsert.='VALUES'.$final_insert_query;
echo $sqlinsert;

INSERT INTO fixture_statistics (id, fixture_id, Shots_on_Goal_home, Shots_on_Goal_away, Shots_of_Goal_home, Shots_of_Goal_away, Total_Shots_home, Total_Shots_away, Blocked_Shots_home, Blocked_Shots_away, Shots_insidebox_home, Shots_insidebox_away, Shots_outsidebox_home, Shots_outsidebox_away, Fouls_home, Fouls_away, Corner_Kicks_home, Corner_Kicks_away, Offsides_home, Offsides_away, Ball_Possession_home, Ball_Possession_away, Yellow_Cards_home, Yellow_Cards_away, Red_Cards_home, Red_Cards_away, Goalkeeper_Saves_home, Goalkeeper_Saves_away, Total_passes_home, Total_passes_away, Passes_accurate_home, Passes _accurate_away, Passes_percentage_home, Passes_percentage_away, create_date) ЗНАЧЕНИЯ (0 ', 3', '5', '2019-11-11 11:13:48'), (0 ', 6', '3', '2019-11-11 11:13:48 '), (0', 13 ',' 9 ',' 2019-11-11 11:13:48 '), (0', 4 ',' 1 ',' 2019-11-11 11:13:48 '), (0', 9 ',' 5 ',' 2019-11-11 11:13:48 '), (0', 4 ',' 4 ',' 2019-11-11 11:13:48 '), (0', 20 ',' 19 ',' 2019-11-11 11:13:48 '), (0', 6 ',' 4 ',' 2019-11-11 11:13:48 '), (0', 5 ',' 1 ',' 2019-11-11 11:13:48 '), (0', 56% ', '44%', '2019-11-11 11:13:48 '), (0', 0 ',' 1 ',' 2019-11-11 11:13:48 '), (0', 0 ',' 0 ',' 2019-11-11 11:13:48 '), (0', 3 ',' 3 ',' 2019-11-11 11:13:48 '), (0', 413 ',' 316 ',' 2019-11-11 11:13:48 '), (0', 265 ',' 176 ',' 2019-11-11 11:13:48 '), (0', 64% ', '56%', '2019-11-1111: 13: 48'

...