简体   繁体   English

在PHP中转换关联数组

[英]Converting associative arrays in PHP

I've been googling around and searching this forum but I haven't actually found a solution for what I need to achieve. 我一直在谷歌搜索和搜索这个论坛,但我实际上并没有找到解决方案,我需要实现的目标。

I'm getting an array from a db in this form 我从这种形式的数据库中获取数组

Array
(
[0] => Array
    (
        [0] => af
        [1] => Afghanistan
    )

[1] => Array
    (
        [0] => al
        [1] => Algeria
    )
)

I need it to look like this 我需要它看起来像这样

Array
(
    [af] => Afghanistan
    [al] => Algeria
)

The db is sql server, this is the code I'm using to get the data db是sql server,这是我用来获取数据的代码

$sQueryCountries = "SELECT Country_Code,Country_Name FROM Countries WHERE Client_Id = '$client_id'";
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );

$rResultCountries = sqlsrv_query( $db, $sQueryCountries, $params, $options );
while ( $aRow = sqlsrv_fetch_array( $rResultCountries, SQLSRV_FETCH_NUMERIC ) )
{       
    $output[] = $aRow;      
}

Pretty basic array building in the fetch loop 在fetch循环中构建非常基本的数组

while ( $aRow = sqlsrv_fetch_array( $rResultCountries, SQLSRV_FETCH_NUMERIC ) )
{       
    $output[$aRow[0]] = $aRow[1];      
}

Try this : 尝试这个 :

$res   = array();
foreach($your_array as $val){
   $res[$val[0]] = $val[1];
}

echo "<pre>";
print_r($res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM