获取MySql数据并放入多维数组



可以是PHP或Mysql解决方案…我希望能够存储一些数据,从mysql数据库中选择了一个php数组。到目前为止,我只能够放入一个"假"数组

$SQL = "SELECT *  FROM continents RIGTH JOIN Country ON Country_Continents = continents_ID";
       while ($CONT = mysql_fetch_array($DataSet)){
            $array_cont[] = $CONT["continents_name"];
            $country_ID_rry [] = $CONT;
         }

然后我得到一个每个大洲的数组在大陆数组中得到国家名称。

Array
(
    [0] => Array
        (
            [0] => America
            [continents_name] => America
            [1] => 3
            [country_id] => 3
            [2] => México
            [country_name] => México
        )
    [1] => Array
        (
            [0] => SouthAmérica
            [continents_name] => SouthAmerica
            [1] => 2
            [country_id] => 2
            [2] => Argentina
            [country_name] => Argentina
        )
    [2] => Array
        (
            [0] => SotuhAmerica
            [continents_name] => SouthAmerica
            [1] => 5
            [country_id] => 5
            [2] => Venezuela
            [country_name] => Venezuela
        )
    [3] => Array
        (
            [0] => SouthAmerica
            [continents_name] => SouthAmerica
            [1] => 6
            [country_id] => 6
            [2] => Colombia
            [country_name] => Colombia
        )
    [4] => Array
        (
            [0] => Caribe
            [continents_name] => Caribe
            [1] => 1
            [country_id] => 1
            [2] => Cuba
            [country_name] => Cuba
        )
)

但是我想要这样的东西…

Array
(
    [SouthAmerica] => Array
        (
            [0] => Argentina
            [1] => Brazil
            [2] => Colombia
        )
    [NorthAmerica] => Array
        (
            [0] => Usa
            [1] => Mexico
            [2] => Canada
        )
    [Europa] => Array
        (
            [0] => Ukraine
            [1] => Germany
            [2] => England
        )
)

警告此扩展在PHP 5.5.0中已弃用,并在PHP 7.0.0中被删除。相反,应该使用MySQLi或PDO_MySQL扩展名。更多信息请参见MySQL:选择API指南和相关FAQ。此函数的替代函数包括:

  • mysqli_fetch_array ()
  • PDOStatement: fetch ()

但是总体思路(获取关联数组):

while ($CONT = mysql_fetch_assoc($DataSet)){
    $Pais_ID_rry[$CONT["continents_name"]][] = $CONT['country_name'];
}

另外,如果您只想要大陆国家名称:

SELECT continents_name, country_name FROM continents . . .

相关内容

  • 没有找到相关文章

最新更新