输出要列出的多维数组

时间:2014-02-11 13:53:21

标签: php

请有人帮忙输出多维数组。

不确定我哪里出错了。 排序顺序看起来正确,但它没有显示结果。

<?php
$atest = Array ( "0" => Array ( "id" => "913", "testname" => "qwerty1", "i" => "1" ),
                 "1" => Array ( "id" => "913", "testname" => "test22", "i" => "2" ),
                 "2" => Array ( "id" => "913", "testname" => "American1", "i" => "3" ),
                 "3" => Array ( "id" => "913", "testname" => "Eagle4", "i" => "4" ) );

$range = range('A','Z');
$output = array();
$output['#'] = array();

foreach($range as $letter){
    $output[$letter] = array();
}

foreach($atest as $test){
    if ($test["testname"] !='') {

        $uc = ucfirst($test["testname"]);
        if(array_search($uc[0], $range) === FALSE){
            $output['#'][] = $uc;
        } else {
            $output[$uc[0]][] = $uc;
        }

    }
}

foreach($output AS $letter => $result){
    echo $letter . "<br/>--------<br/>\n";
    sort($result);
    foreach($result AS $indresult){
        echo '<a href="index.php?option=com_comprofiler&task=page&user=' . (int) $indresult['id'] . '&b=' . $indresult['i'] . '">' . $indresult['testname'] . '</a><br/>';
    }
    echo "<br/>\n";
}
?>

3 个答案:

答案 0 :(得分:0)

尝试使用print_r函数,例如对于数组转储:

print_r($atest);

答案 1 :(得分:0)

试试这个

 $output[$uc[0]][] = $test;

而不是

$output[$uc[0]][] = $uc; //仅存储名称。

请参阅演示here

答案 2 :(得分:0)

您不是将整个子阵列放入$output,而是只放$uc。将中间foreach循环更改为:

foreach($atest as $test){
    if ($test["testname"] !='') {

        $uc = ucfirst($test["testname"]);
        if(array_search($uc[0], $range) === FALSE){
            $output['#'][] = $test;
        } else {
            $output[$uc[0]][] = $test;
        }

    }
}