如何在PDO中基于索引对数组进行排序

时间:2014-04-07 04:46:43

标签: php arrays

我正在使用此代码从数据库中获取所需的结果

$status = 'passed';
$allTest = $conn->prepare('SELECT COUNT(testRunID),platform FROM tooldata WHERE status = :status GROUP BY platform' );
$allTest->execute(array(':status' => $status));

while($row = $allTest->fetch(PDO::FETCH_ASSOC))
    { 
    foreach($row as $key) 
        {
            $totalTest[] = $key;

        } 
    }

print_r($totalTest);

print_r($ totalTest)的输出类似于 [输出] http://pastebin.com/QW46ieYy)。现在我想基于平台对这个数组进行排序,这是奇数索引。如何做到这一点?

在对数组进行排序之后,我只想要偶数索引值是一个单独的数组

1 个答案:

答案 0 :(得分:-1)

$status = 'passed';
$sql = 'SELECT COUNT(testRunID),platform FROM tooldata WHERE status = ? GROUP BY platform';
$stmt = $conn->prepare($sql);
$stmt->execute(array($status));
while($row = $stmt->fetch(PDO::FETCH_ROW))
{ 
    $totalTest[$row[1]] = $row[0]; // sorted by platform which is key
}
$another = $totalTest;
asort($another); // sorted by count