将MySql数据放入多维数组中

时间:2016-11-07 18:57:41

标签: php mysql multidimensional-array

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

$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
        )

)

1 个答案:

答案 0 :(得分:2)

警告此扩展在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。相反,应使用MySQLiPDO_MySQL扩展名。有关详细信息,另请参阅MySQL: choosing an API guiderelated FAQ。该功能的替代方案包括:

  • mysqli_fetch_array()
  • PDOStatement对象::取()

但是一般的想法(只获取关联数组):

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

此外,如果您只想要 Continent 国家名称:

SELECT continents_name, country_name FROM continents . . .