如何在以下场景中更改阵列?

时间:2014-02-18 15:03:50

标签: php arrays associative-array

我的SQL查询如下:

SELECT test_user_user_id FROM OCN.tests_users WHERE test_user_test_id = 99
$this->mDb->Query( $sql );
$students_data = $this->mDb->FetchArray();

我按如下方式获取数组:

Array
(
    [0] => Array
        (
            [test_user_user_id] => b9e6493f9f8599bc5a0a5935275228c2
        )

    [1] => Array
        (
            [test_user_user_id] => 395599c5891c1418357e2efa89bc3e27
        )

    [2] => Array
        (
            [test_user_user_id] => 3255605bb9fd3ecc0295a1bfb3cba147
        )

    [3] => Array
        (
            [test_user_user_id] => ebe6711fc156b2cc1a33f64f3d86150f
        )

    [4] => Array
        (
            [test_user_user_id] => 627b9c3f21d93f1e13af076cff20b143
        )

    [5] => Array
        (
            [test_user_user_id] => 030e96561c01afde1c46384f57cf8749
        )

    [6] => Array
        (
            [test_user_user_id] => 9def02e6337b888d6dbe5617a172c18d
        )
)

其实我想要以下格式的数组:

Array
    (

                [0] => b9e6493f9f8599bc5a0a5935275228c2

                [1] => 395599c5891c1418357e2efa89bc3e27

                [2] => 3255605bb9fd3ecc0295a1bfb3cba147

                [3] => ebe6711fc156b2cc1a33f64f3d86150f

                [4] => 627b9c3f21d93f1e13af076cff20b143

                [5] => 030e96561c01afde1c46384f57cf8749

                [6] => 9def02e6337b888d6dbe5617a172c18d

    )

如何以上述格式获取数组?

3 个答案:

答案 0 :(得分:2)

$resArray = Array();
foreach($myArr as $_arr)
{
   $resArray[] = $_arr['test_user_user_id'];
}

答案 1 :(得分:1)

您可以使用array_map

function user_id($e)
{
    return $e['test_user_user_id'];
}

$students_data = array_map('user_id', $students_data);

答案 2 :(得分:1)

$new_array = array();
$counter = 0; // optional... because by default starting index is 0... however if you want to change the index then use a counter
foreach($students_data as $row) {
    $new_array[$counter] = $row['test_user_user_id'];
    $counter++;
}
print_r($new_array);