array_reverse的问题

时间:2014-11-22 21:12:35

标签: php arrays

我有一个多维数组,打印出来。但它是按照我需要的相反顺序打印的。但是当我尝试使用array_reverse()时,它会输出几乎随机的值:

for($j=0; $j<count($positions);$j++){
    $temp[$j]=strlen($positions[$j]);
}
array_push($GLOBALS['lengths'],$temp);

当我回复$lengths时,我得到(应该是,但顺序相反):

1 1
2 1

但是当我使用array_reverse($GLOBALS['lengths'])时,我得到了:

4 1
2 1

哪里可能是问题?

这就是我使用的代码:

for($j=0; $j<count($positions);$j++){

    $temp[$j]=strlen($positions[$j]);
}
array_push($GLOBALS['lengths'],$temp);
$lengths=array_reverse($GLOBALS['lengths']);

1 个答案:

答案 0 :(得分:1)

试试这个:

//this will reverse the result output
for($j=count($positions); $j>0; $j--){
    array_push($GLOBALS['lengths'],strlen($positions[$j-1]));
}
$lengths=$GLOBALS['lengths'];